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.


[C#]


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");


From http://msdn2.microsoft.com/en-us/library/aa497276.aspx


Cheers,


Anthony Wong [MSFT]

Comments (0)

Skip to main content