Question

I expect the specialfolder.personal returns c:/users/xxx, however in my Windows 7 system, it returns c:/users/xxx/Documents. Why? How to get the folder of personal root directory?

Was it helpful?

Solution

From the documentation:

Personal: The directory that serves as a common repository for documents. This member is equivalent to MyDocuments.

Instead, you want SpecialFolder.UserProfile.

UserProfile: The user's profile folder. Applications should not create files or folders at this level; they should put their data under the locations referred to by ApplicationData.

Update

Apparently, this only works in .NET 4. So instead, try this:

System.Environment.GetEnvironmentVariable("UserProfile");

OTHER TIPS

Have you tried:

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

Edit: this emum member is present only in Framework 4.0. In earler Framework versions, the following should give the same result:

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top