Reputation: 8188
How do I programmically get the File Path of a File In my project?
string fileContent = File.ReadAllText(LocalConstants.EMAIL_PATH);
The EMAIL_PATH I added to my project as a text file. This line of code throws an exception because by default it is looking in the system32 folder for some reason. I need to set it to the projects directory.
Upvotes: 17
Views: 61253
Reputation: 191
This worked for me System.AppDomain.CurrentDomain.BaseDirectory.ToString()
Upvotes: 0
Reputation: 4468
You can use Environment.CurrentDirectory
See: http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx
For further reading.
edit*
string temp = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
Will do the same and is perhaps a little safer.
Upvotes: 0
Reputation: 460018
You could use Path.Combine
and AppDomain.CurrentDomain.BaseDirectory
:
string fileName = "SampleFile.txt";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LocalConstants.EMAIL_PATH, fileName);
Returns in a test project in debug mode this path(when LocalConstants.EMAIL_PATH="Emails"
):
C:\****\****\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsFormsApplication1\bin\Debug\Emails\SampleFile.txt
Upvotes: 31