Actually your code has a bit of a problem, I think, you misplaced
')'. You used it at the end of the whole string instead of using it after 'Location' like this
System.Reflection.Assembly.GetExecutingAssembly().Location + @"\keyfile\EmailbodyorFile.txt");
which in result gives you the location, but with your
\keyfile
.
I tried and run the code to see what path does it return and i got this by running this code:
Console.WriteLine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location + @"\keyfile\EmailbodyorFile.txt"))
the output was:
c:\users\USERNAME\documents\visual studio 2015\Projects\CODEPROJECT\CODEPROJECT\bin\Debug\CODEPROJECT.exe\keyfile
So, The right way to do it is this:
System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\keyfile\EmailbodyorFile.txt";
Which will return this path:
c:\users\USERNAME\documents\visual studio 2015\Projects\CODEPROJECT\CODEPROJECT\bin\Debug\keyfile\EmailbodyorFile.txt
And for checking the existence of directory and file you can use this code:
string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\keyfile";
string filePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\keyfile\EmailbodyorFile.txt";
Start:
if (System.IO.Directory.Exists(dir)) {
if (System.IO.File.Exists(filePath)) {
} else {
System.IO.File.WriteAllText(filePath, "/*your data*/");
goto Start;
}
} else { System.IO.Directory.CreateDirectory(dir); goto Start; }
I hope you understood what I wrote :) .
-Muhammad Muzzammil<br />
-www.facebook.com/muhammadmuzzammil1998<br />
-muhammadmuzzammil.cs@gmail.com