如何找到%APPDATA%目录? [英] How do I find the %APPDATA% directory?

查看:482
本文介绍了如何找到%APPDATA%目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前知道两种方法:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

Application.UserAppDataPath

它们是否都相同?我应该在另一个上使用吗?请提供一些事实来备份您的答案。

Are they both the same? Should I use one over the other? Please provide some facts to back up your answers.

推荐答案

应用程序是WinForms的类。因此,如果您的应用程序不是WinForms App,则不能使用Application.UserAppDataPath。

Application is the class of WinForms. So, if your Application is not WinForms App you cannot use Application.UserAppDataPath.

此外,如果您反编译System.Windows.Forms程序集,则可以看到Application.UserAppDataPath属性使用Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)方法。

Furthermore, if you decompile System.Windows.Forms assembly you can see that Application.UserAppDataPath property use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) method.

public static string UserAppDataPath
{
  get
  {
    try
    {
      if (ApplicationDeployment.IsNetworkDeployed)
      {
        string str = AppDomain.CurrentDomain.GetData("DataDirectory") as string;
        if (str != null)
          return str;
      }
    }
    catch (Exception ex)
    {
      if (System.Windows.Forms.ClientUtils.IsSecurityOrCriticalException(ex))
        throw;
    }
    return Application.GetDataPath(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
  }
}

这篇关于如何找到%APPDATA%目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆