Search code examples
c#installationstreamreaderreadfile

How do I load a file that I bundled with the install of my C# program?


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.


Solution

  • 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"));