[英]How to detect the context from which an application is being executed? Whether from Command Prompt or from within a Windows Form
[英]Detect if my application is being executed from the users directory
我需要检测users
目录中是否正在执行我的应用程序。
例如Windows7上的c:/users
。
但是我需要它在所有Windows版本上都能正常工作。
您可以按以下方式获取当前应用程序的目录:
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
然后,您需要一个字符串比较来查看路径是否以以下内容开头:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
或您要测试的位置,请参见此处以获取特殊文件夹列表。 这些在每个OS版本下都可以正确解决。
bool isUsersPath = System.Reflection.Assembly.GetExecutingAssembly().Location.StartsWith(System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.