Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get File Path of A File In Your Current Project

Tags:

c#

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.

like image 216
Nick LaMarca Avatar asked May 22 '12 14:05

Nick LaMarca


People also ask

How do I find the file path to a document?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I get the current file path in Python?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.

How do I find the path of a file in Visual Studio?

Ctrl+Click or Double Right Click to Open Containing Folder, Right click to Copy Full Path. This lightweight extension lets you display the full path of the file at bottom of Visual Studio's Editor.

What is the path to the file?

A file path describes the location of a file in a web site's folder structure. File paths are used when linking to external files, like: Web pages. Images.


1 Answers

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
like image 109
Tim Schmelter Avatar answered Oct 07 '22 07:10

Tim Schmelter