views:

48

answers:

6

I need to do something like this:

    StreamReader reader = 
new System.IO.StreamReader(@"C:\Program Files\The Awesome Program That I Made\awesomeloadablefile.ldf");

Except I don't know where the user has installed the program. How is my program supposed to know where the installed files are?

I am a noob, in case you hadn't noticed.

+1  A: 

Try something like this.

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

Flesrouy
A: 

Something like Assembly.GetExecutingAssembly().Location should work.

ho1
A: 

You could try this:

 File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "awesomeloadablefile.txt"); 
David Relihan
+3  A: 

You can use Assembly.GetEntryAssembly().Location to get the path on disk of your executable, Path.GetDirectoryName to get the directory it's in, and then Path.Combine to combine the directory name with your file name in that directory. So:

StreamReader reader = new System.IO.StreamReader(Path.Combine(
    Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), 
    "awesomeloadablefile.ldf"));
Quartermeister
+1 for using Path.Combine()
David Crowell
A: 

Assuming you know the directory structure relative to your executable, you can use Application.StartupPath:

string path = Path.Combine(Application.StartupPath, "awesomeloadablefile.ldf");
StreamReader reader = new System.IO.StreamReader(path);
George Howarth
A: 

This will get you a path to the exe directory. I'm assuming that's where you decided to put the file. Otherwise you can specify a had location for it in the installer. Are you using the Visual Studio installer?

Application.StartupPath
Ayubinator