Udostępnij za pośrednictwem


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 nameString fullAppName = Assembly.GetExecutingAssembly().GetName().CodeBase;// This strips off the exe nameString fullAppPath = Path.GetDirectoryName(fullAppName);

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

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

Cheers,

Anthony Wong [MSFT]