Detect if my application is being executed from the users directory
I need to detect if my application is being executed from t开发者_如何学Gohe users
directory.
c:/users
on Windows7.
But i need it to work on all Windows versions.
You can get the directory of the current application as follows:
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
You then need a string compare to see to check if the path startswith:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
Or which ever location you want to test against, see here for list os special folders. These resolve appropriately under each OS version.
bool isUsersPath = System.Reflection.Assembly.GetExecutingAssembly().Location.StartsWith(System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
精彩评论