Search code examples
.netvisual-studiopathrelative-path

Relative paths in Visual Studio


I'm working in Visual Studio 2005 and have added a text file that needs to be parsed by right-clicking the project in the solution explorer and add --> new item. This places the .txt file to the project folder. The debug .exe file is in the /bin/debug folder.

How do I properly point to the txt file from code using relative paths that will properly resolve being two folders back, while also resolving to be in the same folder after the solution is published?


Solution

  • Check out the Application Class. It has several members that can be used to locate files, etc. relative to the application once it's been installed.

    For example, Application.ExecutablePath tells you where the running EXE file is located; you could then use a relative path to find the file e.g. ..\..\FileToBeParsed.txt. However, this assumes that the files are deployed in the same folder structure as the project folder structure, which is not usually the case.

    Consider properties like CommonAppDataPath and LocalUserAppDataPath to locate application-related files once a project has been deployed.