Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#Windows关机过程怪异_C#_Wpf_Windows_Wcf - Fatal编程技术网

c#Windows关机过程怪异

c#Windows关机过程怪异,c#,wpf,windows,wcf,C#,Wpf,Windows,Wcf,我需要一个简单但奇怪的问题的帮助。问题在于从我的应用程序关闭并重新启动windows。我知道这很简单,网上有很多例子,但这里的交易。通过process.start()或win api或ManagementObject等方法执行的关机/重启与单击windows关机或重启按钮执行的关机/重启不同。怎么做 好的,我已经为学校实验室制作了一个客户端应用程序,它可以帮助教师通过屏幕截图和其他事情(如关机、重启、锁定等)来监控桌面上的学生活动。如果我从我的应用程序关机/重启电脑,学生电脑windows启动屏

我需要一个简单但奇怪的问题的帮助。问题在于从我的应用程序关闭并重新启动windows。我知道这很简单,网上有很多例子,但这里的交易。通过process.start()或win api或ManagementObject等方法执行的关机/重启与单击windows关机或重启按钮执行的关机/重启不同。怎么做

好的,我已经为学校实验室制作了一个客户端应用程序,它可以帮助教师通过屏幕截图和其他事情(如关机、重启、锁定等)来监控桌面上的学生活动。如果我从我的应用程序关机/重启电脑,学生电脑windows启动屏幕在学生登录之前不会显示在教师机器上。但是,如果我在学生计算机上从windows菜单执行关机或重新启动,那么学生计算机启动屏幕将显示在教师计算机上

我知道,由我的应用程序启动的关机/重启会完全关闭/重启学生电脑,而windows关机/重启会以某种方式保存用户会话,从而实现某种混合类型的功能。并传递给下一次启动

我想知道如何编写一个代码,将这种行为复制到关机或重启(“某种混合类型的东西,用户会话以某种方式保存,并传递到下一次启动”)。此外,混合动力车也不起作用

客户端应用程序是一个wpf应用程序,它以任务调度器的提升权限运行,并具有启动和登录任务。wpf应用程序最小化到系统托盘,并通过tcp服务器承载wcf服务

关机代码:

var psi = new ProcessStartInfo("shutdown", "/s /t 5");
            psi.CreateNoWindow = true;
            psi.UseShellExecute = false;
            Process.Start(psi);
关闭的其他代码: //发件人:

}

另一个:来自:


好了,大家……终于解决了。如果有人需要这个,就在这里

如果您有一个应用程序在启动时由任务计划程序启动,则只有在您单击windows按钮重新启动或关闭时,它才会在启动时启动。这对我来说是个问题,因为我的应用程序需要在用户登录之前启动,并开始将捕获的屏幕发送到教师电脑

我使用了stackoverflow和其他站点提到的各种关机方法,它们工作正常,但不像windows的关机和重启按钮工作

下面是复制这种行为的方法

如果查看“shutdown”命令在命令提示符中的参数,它会提到:

/g:完全关机并重新启动计算机。系统重新启动后,重新启动所有已注册的应用程序

/sg:关闭计算机。下次启动时,重新启动所有已注册的应用程序


“重新启动任何已注册的应用程序”,那么您使用哪种APi?你的密码在哪里?您是否将程序设置为“启动”,以便在windows登录时自动启动?更新了帖子
void Shutdown()
{
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. Use "2" to reboot.
mboShutdownParams["Flags"] = "1";
mboShutdownParams["Reserved"] = "0";
foreach (ManagementObject manObj in mcWin32.GetInstances())
{
    mboShutdown = manObj.InvokeMethod("Win32Shutdown", 
                                   mboShutdownParams, null);
}
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=1)]
internal struct TokPriv1Luid
{
public int Count;
public long Luid;
public int Attr;
}

[DllImport("kernel32.dll", ExactSpelling=true) ]
internal static extern IntPtr GetCurrentProcess();

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool OpenProcessToken( IntPtr h, int acc, ref IntPtr
phtok );

[DllImport("advapi32.dll", SetLastError=true) ]
internal static extern bool LookupPrivilegeValue( string host, string name,
ref long pluid );

[DllImport("advapi32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool AdjustTokenPrivileges( IntPtr htok, bool disall,
ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen );

[DllImport("user32.dll", ExactSpelling=true, SetLastError=true) ]
internal static extern bool ExitWindowsEx( int flg, int rea );

internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
internal const int TOKEN_QUERY = 0x00000008;
internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
internal const int EWX_LOGOFF = 0x00000000;
internal const int EWX_SHUTDOWN = 0x00000001;
internal const int EWX_REBOOT = 0x00000002;
internal const int EWX_FORCE = 0x00000004;
internal const int EWX_POWEROFF = 0x00000008;
internal const int EWX_FORCEIFHUNG = 0x00000010;

private void DoExitWin( int flg )
 {
bool ok;
TokPriv1Luid tp;
IntPtr hproc = GetCurrentProcess();
IntPtr htok = IntPtr.Zero;
ok = OpenProcessToken( hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref 
htok );
tp.Count = 1;
tp.Luid = 0;
tp.Attr = SE_PRIVILEGE_ENABLED;
ok = LookupPrivilegeValue( null, SE_SHUTDOWN_NAME, ref tp.Luid );
ok = AdjustTokenPrivileges( htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero 
);
ok = ExitWindowsEx( flg, 0 );
}
var psi = new ProcessStartInfo("shutdown", "/sg /t 0");
        psi.CreateNoWindow = true;
        psi.UseShellExecute = false;
        Process.Start(psi);
var psi = new ProcessStartInfo("shutdown", "/g /t 0");
        psi.CreateNoWindow = true;
        psi.UseShellExecute = false;
        Process.Start(psi);