How do I find the %APPDATA% directory?

北城余情 提交于 2019-12-13 10:42:11

问题


I currently know of two methods:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

and

Application.UserAppDataPath

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


回答1:


Application.UserAppDataPath returns BasePath\CompanyName\ProductName\ProductVersion, where BasePath is the ApplicationData directory. So if you don't want all the extra subdirectories, you should just use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).




回答2:


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

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));
  }
}


来源:https://stackoverflow.com/questions/9561538/how-do-i-find-the-appdata-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!