c# 修改windows中账户的用户名和密码
在C#中,我们可以使用WMI类中的Win32_Service或者Win32API中的函数ChangeServiceConfig来修改本地或远程计算机Windows服务登录身份(账户)的用户名和密码。
1、使用Win32API修改服务登录身份信息:
使用Win32API中的函数 ChangeServiceConfig 更改的是服务控制管理器数据库中指定服务的配置信息。
privateconstintSC_MANAGER_ALL_ACCESS=0x000F003F;
privateconstuintSERVICE_NO_CHANGE=0xffffffff;//这个值可以在winsvc.h中找到
privateconstuintSERVICE_QUERY_CONFIG=0x00000001;
privateconstuintSERVICE_CHANGE_CONFIG=0x00000002;
[DllImport("advapi32.dll",CharSet=CharSet.Unicode,SetLastError=true)]
publicstaticexternBooleanChangeServiceConfig(IntPtrhService,UInt32nServiceType,
UInt32nStartType,UInt32nErrorControl,StringlpBinaryPathName,StringlpLoadOrderGroup,
IntPtrlpdwTagId,[In]char[]lpDependencies,StringlpServiceStartName,
StringlpPassword,StringlpDisplayName);
[DllImport("advapi32.dll",SetLastError=true,CharSet=CharSet.Auto)]
staticexternIntPtrOpenService(IntPtrhSCManager,stringlpServiceName,uintdwDesiredAccess);
[DllImport("advapi32.dll",EntryPoint="OpenSCManagerW",ExactSpelling=true,
CharSet=CharSet.Unicode,SetLastError=true)]
publicstaticexternIntPtrOpenSCManager(stringmachineName,stringdatabaseName,uintdwAccess);
publicstaticboolChangeServiceAccountInfo(stringserviceName,stringusername,stringpassword)
{
try
{
IntPtrscm_Handle=OpenSCManager(null,null,SC_MANAGER_ALL_ACCESS);
if(scm_Handle==IntPtr.Zero)
thrownewSystem.Runtime.InteropServices.ExternalException("打开服务管理器错误");
IntPtrservice_Handle=OpenService(scm_Handle,serviceName,SERVICE_QUERY_CONFIG|SERVICE_CHANGE_CONFIG);
if(service_Handle==IntPtr.Zero)
thrownewSystem.Runtime.InteropServices.ExternalException("打开服务错误");
//修改服务的账户用户名和密码
if(!ChangeServiceConfig(service_Handle,SERVICE_NO_CHANGE,SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,null,null,IntPtr.Zero,null,username,password,null))
{
intnError=Marshal.GetLastWin32Error();
Win32Exceptionwin32Exception=newWin32Exception(nError);
thrownewSystem.Runtime.InteropServices.ExternalException("无法修改服务登录身份的用户名和密码:"+win32Exception.Message);
}
Console.WriteLine("服务登录身份信息修改成功!");
returntrue;
}
catch(Exceptionex)
{
Console.WriteLine(ex.ToString());
returnfalse;
}
}
2、使用C#中WMI修改服务登录身份信息:
使用WMI服务,我们需要添加System.Management的引用。
注意:如果您的远程计算机连接的是ActiveDirectory域,那么使用完全限定的用户名(例如TestDomainMorgan)而不是简单的用户名(Morgan)。
usingSystem.Management;
publicstaticvoidChangeServiceAccountInfobyWMI(stringserviceName,stringusername,stringpassword)
{
stringmgmntPath=string.Format("Win32_Service.Name='{0}'",serviceName);
using(ManagementObjectservice=newManagementObject(newManagementPath(mgmntPath)))
{
object[]accountParams=newobject[11];
accountParams[6]=username;
accountParams[7]=password;
uintreturnCode=(uint)service.InvokeMethod("Change",accountParams);
if(returnCode==0)
{
Console.WriteLine("服务登录身份信息修改成功!");
}
else
{
Console.WriteLine("服务登录身份信息修改失败");
Console.WriteLine("错误代码:"+returnCode);
//此微软官方支持链接,可以查看相应的返回代码的消息:
//https://msdn.microsoft.com/en-us/library/aa393660(v=vs.85).aspx
}
}
}
3、使用C#中的WMI修改远程计算机服务的登录身份信息:
使用WMI服务,我们需要添加System.Management的引用,并且在修改远程计算机中的服务信息时,请使用管理员凭据。
注意:如果您的远程计算机连接的是ActiveDirectory域,那么使用完全限定的用户名(例如TestDomainMorgan)而不是简单的用户名(Morgan)。
usingSystem.Management;
staticvoidChangeRemoteServiceAccountInfo(stringremoteComputer,stringserviceName,stringusername,stringpassword)
{
try
{
ConnectionOptionsconnectionOptions=newConnectionOptions();
//如需要,请使用证书
//connectionOptions.Username="Administrator";
//connectionOptions.Password="AdminPassword";
//connectionOptions.Impersonation=ImpersonationLevel.Impersonate;
ManagementScopescope=newManagementScope("\"+remoteComputer+"rootCIMV2",connectionOptions);
scope.Connect();
stringmgmntPath=string.Format("Win32_Service.Name='{0}'",serviceName);
using(ManagementObjectservice=newManagementObject(scope,newManagementPath(mgmntPath),newObjectGetOptions()))
{
object[]accountParams=newobject[11];
accountParams[6]=username;
accountParams[7]=password;
uintreturnCode=(uint)service.InvokeMethod("Change",accountParams);
if(returnCode==0)
{
Console.WriteLine("服务登录身份信息修改成功!");
}
else
{
Console.WriteLine("服务登录身份信息修改失败");
Console.WriteLine("错误代码:"+returnCode);
//此微软官方支持链接,可以查看相应的返回代码信息:
//https://msdn.microsoft.com/en-us/library/aa393660(v=vs.85).aspx
}
}
}
catch(Exceptionex)
{
Console.WriteLine(ex.ToString());
}
}
以上就是c#改变windows中账户的用户名和密码的详细内容,更多关于c#更改用户名和密码的资料请关注毛票票其它相关文章!