问题
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