7

I've looked at the Environment.GetFolderPath method and the System.Environment.SpecialFolder enum but I couldn't see anything that returns the path of the Default Users folder.

Can someone please tell me how to get the Default Users folder (or even better the Default Users AppData Local folder path e.g. c:\users\Default\AppData\Local) programmatically as I need to copy some files into this folder?

Thank you

m_collard
  • 2,008
  • 4
  • 29
  • 51

3 Answers3

6

There are lots of articles on the web that describe how to change the Default User Profile path:

http://support.microsoft.com/kb/214636

http://www.nextofwindows.com/how-to-change-user-profile-default-location-in-windows-7/

They all say the current Default Profile Path is stored in the following registry location:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList

e.g. %SystemDrive%\Users\Default

And I found this page to get the System Drive: How to get current windows directory e.g. C:\ in C#

Path.GetPathRoot(Environment.SystemDirectory)

So I'm going to use that. Thanks for your help.

UPDATE

I've just tried the following code and it returns C:\Users\Default. So there is no need to replace the %SystemDrive% text stored in the registry key. It replaces it automatically.

using (RegistryKey profileListKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"))
{
    string defaultPath = profileListKey.GetValue("Default").ToString();
}
Community
  • 1
  • 1
m_collard
  • 2,008
  • 4
  • 29
  • 51
3

Snippet from LINQPad (Language: C# Program) that outputs 'C:\Users\Default\Desktop':

void Main()
{
    GetFolderPath(Environment.SpecialFolder.Desktop).Dump();
}

// Define other methods and classes here
[DllImport("shfolder.dll", CharSet=CharSet.Auto)]
internal static extern int SHGetFolderPath(IntPtr hwndOwner, int nFolder, int hToken, int dwFlags, StringBuilder lpszPath);

public static string GetFolderPath(Environment.SpecialFolder folder)
{
    if (!Enum.IsDefined(typeof(Environment.SpecialFolder), folder))
    {
        throw new Exception("Crap");
    }
    StringBuilder lpszPath = new StringBuilder(260);

    SHGetFolderPath(IntPtr.Zero, (int) folder, -1, 0, lpszPath);
    string path = lpszPath.ToString();
    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, path).Demand();
    return path;
}

Edit: I had the following imports in LINQPad

System.Runtime.InteropServices
System.Globalization
System.Security.Permissions

I used reflector to look at Environment.GetFolderPath and then took a look at SHGetFolderPath that specifies by passing -1 as hToken you get Default User instead.

Don
  • 9,511
  • 4
  • 26
  • 25
  • 2
    SHGetFolderPath -- "Assigning the hToken parameter a value of -1 indicates the Default User. This enables clients of SHGetFolderPath to find folder locations (such as the Desktop folder) for the Default User. The Default User user profile is duplicated when any new user account is created, and includes special folders such as My Documents and Desktop. Any items added to the Default User folder also appear in any new user account." -- http://msdn.microsoft.com/en-us/library/windows/desktop/bb762181(v=vs.85).aspx – Mike Clark Jun 14 '13 at 11:45
  • I wouldn't recommend using p/Invoke nowadays. – JoanComasFdz Jun 14 '13 at 12:25
  • @JoanComasFdz so how would you recommend getting it instead? – Scott Chamberlain Jun 14 '13 at 13:40
  • @JoanComasFdz, there's nothign wrong with using pinvoke, especially if you're accessing functionality not exposed by the .NET framework (it's unnecessary for functionality that is exposed). – Samuel Neff May 06 '14 at 19:12
0

You can't because the access to that folder is denied, this folder is used only by the Microsoft. I sure Environment or any other class will not provide you such functionality. This can only be done with some sort of hacking maybe?

a1204773
  • 6,923
  • 20
  • 64
  • 94
  • 1
    Permissions are based on the context the application is running in. An admin can certainly access the folder. – Samuel Neff May 06 '14 at 19:14