2

我希望 specialfolder.personal 返回 c:/users/xxx,但是在我的 Windows 7 系统中,它返回 c:/users/xxx/Documents。为什么?如何获取个人根目录的文件夹?

4

2 回答 2

4

文档中:

个人:用作文档公共存储库的目录。此成员相当于 MyDocuments。

相反,你想要SpecialFolder.UserProfile.

UserProfile:用户的个人资料文件夹。应用程序不应在此级别创建文件或文件夹;他们应该将他们的数据放在 ApplicationData 引用的位置下。

更新

显然,这只适用于 .NET 4。所以,试试这个:

System.Environment.GetEnvironmentVariable("UserProfile");
于 2010-11-10T04:56:00.657 回答
1

你有没有尝试过:

Environment.GetFolderPath (System.Environment.SpecialFolder.UserProfile)

编辑:这个 emum 成员仅存在于 Framework 4.0 中。在较早的框架版本中,以下应该给出相同的结果:

void Main()
{
    var lpszPath = new StringBuilder(260);
    const int UserProfile = 40;
    SHGetFolderPath (IntPtr.Zero, UserProfile, IntPtr.Zero, 0, lpszPath);
    string answer = lpszPath.ToString();    
}

[DllImport("shfolder.dll", CharSet=CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, IntPtr hToken, int dwFlags, StringBuilder lpszPath);
于 2010-11-10T04:55:24.363 回答