Opening a file from application location

  • Thread starter Thread starter grant
  • Start date Start date
G

grant

Good day,

How do I open a text file from the applications install location?
I am currently opening a text file using "StreamReader sr = new
StreamReader("DB.txt");" and then reading from that file.

Problem is if I create a shortcut on the users desktop it tries to read that
file from the desktop when I need it to read from the actual location of the
executable.

Thanks,
Grant
 
The name of the file "DB.txt" implies to me that you are wish to write to
this file, am I correct?
 
Awesome, thanks.

Peter Duniho said:
You can get path information for the executable from the assembly. See
the Assembly class for details.

That said, if you have control over the install process (and why wouldn't
you? :) ), it seems to me that you may be better off storing a registry
key value specifying the file's path (and maybe even its name) during the
installation and retrieving that information at run-time. That would work
just as well, and would generalize the code so that fewer changes have to
be made to the code in the future if the location of the file changes.

Pete
 
Back
Top