Opening a file from application location

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
 
P

Peter Morris

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

grant

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top