【发布时间】:2011-08-31 12:42:15
【问题描述】:
我需要检测我的应用程序是否正在从users 目录执行。
例如 Windows7 上的 c:/users。
但我需要它在所有 Windows 版本上工作。
【问题讨论】:
我需要检测我的应用程序是否正在从users 目录执行。
例如 Windows7 上的 c:/users。
但我需要它在所有 Windows 版本上工作。
【问题讨论】:
可以通过如下方式获取当前应用的目录:
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
然后你需要一个字符串比较来检查路径是否以:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
或者您要测试的任何位置,请参阅here 以获取列表操作系统特殊文件夹。这些会在每个操作系统版本下适当地解决。
【讨论】:
bool isUsersPath = System.Reflection.Assembly.GetExecutingAssembly().Location.StartsWith(System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
【讨论】: