3

I've made a small console application (first publish in c#). but i cant use my resource files. I used textfiles can give it. It worked when i used the debug directory

My goal is to create a directory like this: Applicationmap + application.exe + setup resource map + configurations.txt Logger + .

Now if i try to reach the configuration file it sends me to: C:\Users\<username> \AppData\Local\Apps\2.0\D01L7N51.9EW\R7HB7NAB.B7Y \sele..tion_0000000000000000_0001.0000_92af5262ce6f49d8

While i'm expecting C:/Users/<username>/ Documents/<application>/ + resources/config.txt.

I've tried

string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Console.WriteLine(dir);

&&

Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

&&

Console.WriteLine(Directory.GetCurrentDirectory());

but i always end up at the appdata map.

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
LMP
  • 53
  • 6
  • Where is your application running from (what folder)? – mjwills Dec 04 '17 at 11:07
  • @mjwills The application is published in to the mydocuments map, but i want to make it possible to place it anywhere, so to access the txt files i'm trying reach the application folder. I think it's "running" in the appdata folder somewhere tho. (i have no experience with c# publishing yet). i don't know if i can, or how to change it. – LMP Dec 04 '17 at 12:07

2 Answers2

0

How about:

System.IO.Path.GetDirectoryName(Application.ExecutablePath);
Nimrod P.
  • 150
  • 5
0

You could simply use Application.StartupPath (reference), but the outcome will always depend on where you place/install the executable file

If you always want to point the current user's Documents folder (which is registered as a special folder within the operating system), like in your example, you could either use:

String path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

or (but at your own risk since its much less safe):

String path = Path.Combine(Environment.ExpandEnvironmentVariables("%userprofile%"), "Documents");

Once you have retrieved the correct Documents folder path with either the first or the second approach, combine it with the last part:

path = Path.Combine(path, "resources/config.txt");

For more information concerning the two approaches:

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • Application.StartupPath only works for forms applications :/ Not for console. The other options are very helpfull thanks. I wanted to make the application easy accessible (sinse the user needs to provide the txt files.) so it is possible with these codes but i thought it would be "fool safer" if i could let the users place there txt config files in the application folder (so they can move it anywhere(desktop/documents/subfolders ect) – LMP Dec 04 '17 at 12:02
  • I think the first option(s) is the best one. Glad to be helpful. If you think my answered solved your problem, please mark it as accepted! Thanks! – Tommaso Belluzzo Dec 04 '17 at 18:46