How do I determine the application’s root directory?

This is a popular question in the forum. The answer used be part of NETCF FAQ but cannot be easily located these days, so I would include the information here: 

An application can determine the directory from which it was run by utilizing Reflection and easily modify it using IO.Path namespace.


using System.Reflection;
using System.IO;

// This is the full directory and exe name
String fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;

// This strips off the exe name
String fullAppPath = Path.GetDirectoryName(fullAppName);

// This adds a file name to the path
String fullFileName = Path.Combine(fullAppPath, "myfile.txt");



Anthony Wong [MSFT]

Skip to main content