-2

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.

CJ7
  • 22,579
  • 65
  • 193
  • 321

2 Answers2

2

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).

Michael Liu
  • 52,147
  • 13
  • 117
  • 150
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));
  }
}
Victor Chekalin
  • 676
  • 1
  • 8
  • 15
  • Not quite, as it is in System.Windows.Forms.dll, which is installed with every flavor of .NET. there are some instances where you *couldn't* use it (e.g., silverlight), but in those cases the other option isn't available as well. –  Mar 05 '12 at 17:05