Relative paths in Visual Studio

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.


An alternative to locating the file on disk would be to include the file directly in the compiled assembly as an embedded resource. To do this, right-click on the file and choose "Embedded Resource" as the build action. You can then retrieve the file as a byte stream:

Assembly thisAssembly = Assembly.GetExecutingAssembly();
Stream stream = thisAssembly.GetManifestResourceStream("Namespace.Folder.Filename.Ext");
byte[] data = new byte[stream.Length];
stream.Read(data, 0, (int)stream.Length);

More information about embedded resources can be found here and here.

If you're building an application for your own use, David Krep's suggestion is best: just copy the file to the output directory. If you're building an assembly that will get reused or distributed, then the embedded resource option is preferable because it will package everything in a single .dll.


If I understand your question correctly: Select the file in the "Solution Explorer". Under properties --> Copy to Output Directory, select "Copy Always" or "Copy if Newer". For build action, select "Content". This will copy the file to the /bin/debug folder on build. You should be able to reference it from the project root from then on.