Take the 2-minute tour ×

I want to use a data file and read it from a WinForms app. If I use methods like Application.ExecutablePath or Application.StartupPath they reference the folder ..\bin\debug, which is different in non VS hosted app. I want to use a folder off the root folder and get a reference to it regardless if I am in debug mode or not, ie, it always works right.

What's a good way to do it? I prefer not to use an embedded resource file and not use hard coded full paths. It needs to be a relative path off the app's root folder.

Something like ~/App_Data in ASP.NET


I want to use a relative path off "some path". "Some path" is what I am seeking, regardless if I am in VS in debug mode, non debug mode or if the app is deployed. Just like ASP.NET's "~". It doesn't care where the site is or if you're in VS. I don't want to create a folder under 'debug' folder. It's the same folder no matter what. "Some folder" is also off the VS's app folder because the app will be zipped to be used without deployment or publishing.

share|improve this question
What is the "app's root folder"? You are not going to ship your project to your customer. It actually is Path.GetDirectoryName(StartupPath), it correctly gives the program location no matter where's it is located or how it is hosted. Copy your data there too. Supported by the build system with Copy to Output Directory. –  Hans Passant Oct 31 '11 at 11:29

4 Answers 4

Try accessing the location of the current executing assembly:

string path = System.Reflection.Assembly.GetExecutingAssembly().Location
string fullFilePath = Path.Combine(path, "myfile");
..do something with your data file.

This gives you the directory where your EXE/DLL assembly is been executed. Then you could create a subdirectory for your data files. This sometiomes does not work with unit tests framework.

share|improve this answer
This wouldn't work if the program were installed to something like \Program Files... User might not have necessary rights to that dir –  L.B Oct 31 '11 at 7:49
It points to debug\bin when I am in VS debug mode. No different than the examples I posted. –  Tony_Henrich Oct 31 '11 at 16:21
up vote 1 down vote accepted

My solution. Go up two folder levels and then to data folder:

    string appPath = Path.GetDirectoryName(Application.ExecutablePath);

    System.IO.DirectoryInfo directoryInfo = System.IO.Directory.GetParent(appPath);
    System.IO.DirectoryInfo directoryInfo2 = System.IO.Directory.GetParent(directoryInfo.FullName);

    string path = directoryInfo2.FullName + @"\data";
share|improve this answer

ExecutablePath will work right as long as your data files will be in the bin also in release and prod deployment. The point is more that usually that is not the best place to have changing files like databases in that case I think you should use SpecialFolders like ApplicationData.

share|improve this answer

You probably want to use the Environment.GetFolderPath method and pass it one of the enums for the various sytem data folders. A full call might look like

return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
share|improve this answer

Your Answer


