1

I have a file that I store some site links. Until now I used:

string path = Environment.CurrentDirectory + "/forumlinks.txt";

But I want to store the file in the release folder so I can change it and I know it will change permanently for the user.

So I changed to this:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/forumlinks.txt";

But I get an exception:

System.IO.FileNotFoundException.

My question: is this the right way to get the file from the release folder? Should I rethink that and store him in a different place? If so I will be glad to hear about it.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
samy
  • 1,949
  • 6
  • 39
  • 64

4 Answers4

4

I don't see the reason why you would need to call the

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) 

at all. This does the trick:

AppDomain.CurrentDomain.BaseDirectory
Jay
  • 18,959
  • 11
  • 53
  • 72
Goran Štuc
  • 581
  • 4
  • 15
  • when i publish the app and runing for the first time, the file is not found, but afther i use the rewrite function, it create it and i can get it (empty) with the your Suggestion. why is that? – samy Oct 17 '12 at 22:19
  • well if the file does not exist you cant open it, my suggestion is to make sure it exists before you try to open it or, which would be better: check if it exists and if it doesn't create it – Goran Štuc Oct 17 '12 at 22:24
  • the problem is i want to ship the app with some data. the user can delete is if he want but i still want to have the like a basic stuff inside. is that possible in my situation? – samy Oct 17 '12 at 22:26
  • so, the file exists, but the application can't find it? are you sure you are calling the right event in asp? – Goran Štuc Oct 17 '12 at 22:31
1

Well assuming the forward / is a typo. If you've added the test file to your project, check it's properties, needs to be copy if newer / copy always to put it in bin\Debug or bin\Release, with the exe and dlls and other gubbins.

Why are you doing it this way, are you planing for something to change the file, without having to rebuild the application?

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
  • yeah it was a typo, i guess im tierd. i didnt notice that i didnt move the file to the folder lol (coding late at night) – samy Oct 17 '12 at 21:47
1

Hard coding a path to your code release folder is dangerous. What if you want to build in debug mode for some reason? What happens when you want to deploy your program?

A better choice would be to use the environment's application data folder. This will be different for each user, so it means that each user can have their own version of the file.

See this post for details of how to get the application data folder.

Community
  • 1
  • 1
Steztric
  • 2,832
  • 2
  • 24
  • 43
0

i made a mistake and found out that it was right to use this code:

string path =  System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\fourmlinks.txt";

if you put the right name of the file, and still get an exception that mean that the file is not in the correct place.

samy
  • 1,949
  • 6
  • 39
  • 64