2

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?

Spike
  • 2,016
  • 12
  • 27
user496949
  • 83,087
  • 147
  • 309
  • 426

2 Answers2

4

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");
jordanbtucker
  • 5,768
  • 2
  • 30
  • 43
1

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);
Joe Albahari
  • 30,118
  • 7
  • 80
  • 91