views:

81

answers:

4

Hi

I am reading a file using File.ReadAllText("filename.txt"). This file is located in very same folder where the .exe file is located (c:/program files/installation folder). But windows app looks into System32 folder for this file by default.

**I don't want to hard code the path of the file.

+2  A: 
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );

Gives the directory of the exe. So using that with Path.Combine will give the desired result:

string filenamelocation = System.IO.Path.Combine(path, "filename.txt");
PoweRoy
A: 

So you need directory of your application? In that case, there are some good answers already on SO, for example:

http://stackoverflow.com/questions/938421/getting-the-applications-directory-from-a-wpf-application

A: 

Try this function:

using System.Reflection;

private string MyDirectory()
    {
        return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    }

Then use

File.ReadAllText(MyDirectory() + @"\filename.txt");
Longball27
A: 

A lot shorter

File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + "filename.txt");
Bastiaan Linders