Jump to content
Xtreme .Net Talk

LoadUserProfile not creating local user profile when it does not exist [C#]


Recommended Posts

Posted

I have a tool which I use to programmatically create local user accounts as follows:

 


DirectoryEntry NewUser = dirEntryLocalMachine.Children.Add("UserName", "user");
NewUser.Invoke("SetPassword", new object[] { "Passsord" });
NewUser.Invoke("Put", new object[] { "Description", "Description" });
NewUser.CommitChanges();
[/Code]

 

The account is created fine but at at this point the User Profile does not exists (no HKEY CURRENT USER, no Documents & Settings, etc...), I was doing some research into this and found the following MSDN article that says calling LoadUserProfile(...) will actually create the profile if it does not exist:

http://support.microsoft.com/kb/196070/en-us

 

So I added the code as follows:

[Code]
IntPtr hToken = IntPtr.Zero;
bool bLogon = LogonUser(
sName,
sDomain,
sPassword,
LOGON32_LOGON_NETWORK,
LOGON32_PROVIDER_DEFAULT,
out hToken
);

PROFILEINFO profileInfo = new PROFILEINFO();
profileInfo.dwSize = Marshal.SizeOf(profileInfo);
profileInfo.dwFlags = 1;
profileInfo.lpUserName = sName;
bool bLoad = LoadUserProfile(hToken, ref profileInfo);
[/Code]

 

Now, both bLogon and bLoad are true, no exceptions occur, everything "seems" to work fine ...

The contents of profileInfo are not updated (I would have assumed field like .lpProfilePath should have good values) and GetUserProfileDirectory() fails to find the path (obviously - it doesn't exist) - I also check manually and there is nothing under "documents & settings" for the new account.

 

Anyone have any clues as to what I am doing wrong?

 

Any help would be much appreciated.

Thanks,

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...