【问题标题】:Detect if my application is being executed from the users directory检测我的应用程序是否正在从用户目录执行
【发布时间】:2011-08-31 12:42:15
【问题描述】:

我需要检测我的应用程序是否正在从users 目录执行。
例如 Windows7 上的 c:/users

但我需要它在所有 Windows 版本上工作。

【问题讨论】:

    标签: c# directory detect


    【解决方案1】:

    可以通过如下方式获取当前应用的目录:

    string appPath = Path.GetDirectoryName(Application.ExecutablePath);
    

    然后你需要一个字符串比较来检查路径是否以:

    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    

    或者您要测试的任何位置,请参阅here 以获取列表操作系统特殊文件夹。这些会在每个操作系统版本下适当地解决。

    【讨论】:

      【解决方案2】:
      bool isUsersPath = System.Reflection.Assembly.GetExecutingAssembly().Location.StartsWith(System.Environment.GetFolderPath(Environment.SpecialFolder.UserProfile))
      

      【讨论】:

        猜你喜欢
        • 2016-01-15
        • 1970-01-01
        • 2019-11-22
        • 2013-07-12
        • 2021-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多