关闭锁定机器的代码 [英] Code That Shuts Down A Machine Thats Locked
问题描述
以下代码关闭了我的计算机.我希望能够在机器锁定时关闭XP盒.
未锁定机器时,此代码有效.机器锁定时如何使它工作?
private void shutDown()
{
/>//MessageBox.Show(shutting down"); = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get(); />/> ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
////标志1表示我们要关闭系统
s" mboShutdownParams" />/> mboShutdownParams ["Reserved"] ="0"; mboShutdownParams,null);
}
}
The following code shuts down my computer. I would like to be able to shut down my XP box when the machine is locked.
This code works when the machine is not locked. How can I get it to work when the machine is locked?
private void shutDown()
{
//MessageBox.Show("shutting down");
ManagementBaseObject mboShutdown = null;
ManagementClass mcWin32 = new ManagementClass("Win32_OperatingSystem");
mcWin32.Get();
// You can't shutdown without security privileges
mcWin32.Scope.Options.EnablePrivileges = true;
ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters("Win32Shutdown");
// Flag 1 means we want to shut down the system
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
mboShutdown = manObj.InvokeMethod("Win32Shutdown", mboShutdownParams, null);
}
}
推荐答案
尝试强制关闭计算机,我用此方法强制关闭计算机,请使用下面的代码也许可以解决问题.我在vb程序中使用了代码,但是我只是通过在线转换器将其转换为c#.
Try to force the computer to shut down, i used this to force computer to shutdown, use the code below maybe it solve it. I used the code in my vb program but i just convert it to c# with online converter
public object ForceComputerToShutDown()
{
float t = 0;
object objWMIService = null;
object objComputer = null;
objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}");
foreach (var objComputer in objWMIService.InstancesOf("Win32_OperatingSystem")) {
t = objComputer.Win32Shutdown(1 + 4, 0);
if (t != 0) {
MessageBox.show("Error occurred!!!");
}
else {
}
}
}
kaymaf
这篇关于关闭锁定机器的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!