1

I am looking for a simple method to modify a local user's properties in windows 7 using a simple c# application.

I am not familiar with many of the Windows system libraries and am looking to accomplish each of the following tasks:

  1. Enable/Disable a local user account in windows.
  2. Change the password of a local user account in windows.

Some example code and an simple explanation of what the code does would be very helpful.

Edit:

I will have administrative access to the machine when this program is going to be run.

Community
  • 1
  • 1
  • Hmmm... Sounds malicious to me. I can't think of a reason you'd want to do this that isn't for doing something bad. And Win7 requires you be logged in as an administrative user to do either of these things. – Ken White Mar 14 '11 at 19:59
  • This is not malicious, it is meant for a class project, I will have administrative access when this program is executed. – abhishekpradhan Mar 14 '11 at 20:09
  • These settings can be modified via NetUserSetInfo and USER_INFO_1; not sure if there is a managed equivalent or not (probably is). – Luke Mar 14 '11 at 20:26

2 Answers2

1

If you have access this will give you access to what you need.

DirectoryEntry localDirectory = new DirectoryEntry("WinNT://"Environment.MachineName.ToString());
DirectoryEntries users = localDirectory.Children;
DirectoryEntry user = users.Find("userName");

Here is a link to the docs..

http://msdn.microsoft.com/en-us/library/system.directoryservices.directoryentry.aspx

BentOnCoding
  • 27,307
  • 14
  • 64
  • 92
1

I have had a really great experience using the System.DirectoryServices.AccountManagement namespace. It lets you do all the stuff you want to do with accounts without needing to mess with magic strings.

System.DirectoryServices.AccountManagement namespace: http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.aspx

The key entry point to look at is the PrincipalContext class.

Garo Yeriazarian
  • 2,533
  • 18
  • 29