Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location + @"\keyfile\EmailbodyorFile.txt");


I used this to get the path,but my problem is 'EmailbodyorFile.txt' must be created automatically during runtime and the above code do not create the file.So what to do?


Earlier I used this code ,
C#
string path = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory + @"\keyfile\EmailbodyorFile.txt");

and it run correctly,but after creating setup of project, it shows error 'File not found error'.

please help.
Posted
Updated 5-Oct-15 18:53pm
v2
Comments
George Swan 6-Oct-15 3:01am    
Could I suggest that you do the following? Use the 'dot notation' to locate the file.
var text = File.ReadAllLines(@".\MyFolder\MyFile.txt");
This reads 'MyFile.txt' from the 'executing assembly'\MyFolder
Also, use a try catch block to catch FileNotFoundException

You can make use of File.Create method to create/overwrite the file at desired path.
 
Share this answer
 
v2
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
C#
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:
C#
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:
C#
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:
C#
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)) {
      //DoStuff();
    } 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
 
Share this answer
 
Comments
sayana3 6-Oct-15 3:39am    
thanks.It worked nicely.But problem is after publishing my application ,its not working.
not able to reach the path.was there any other method?
muhammadmuzzammil1998 6-Oct-15 4:12am    
okay so you are saying that, on your computer, it's working but not on other computers.
can you answer my few questions?
1). did you wrote code for file creation if file does not exists?
2). what's the location of the file that you are reading and the file that your program wrote? please give me the exact code that you are using to get the path.

please clean build the program and then try again... :)

-Muhammad Muzzammil
-www.facebook.com/muhammadmuzzammil1998
-muhammadmuzzammil.cs@gmail.com
muhammadmuzzammil1998 15-Oct-15 15:21pm    
Did it worked? :) @sayana3
sayana3 16-Oct-15 3:02am    
Ya ,it worked.The problem is the directory is not creating after setup.So as you said I included The code to check whether file exists or not.Then it was ok.Thanks a lot.
muhammadmuzzammil1998 17-Oct-15 22:38pm    
I am glad that it worked :)
Can you mark this question as solved to others can get help too? :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900