启动自重启的 C# 代码?

发布于 2024-09-18 19:15:45 字数 210 浏览 6 评论 0原文

我想编写一个程序,该程序将在特定的 Web 服务器上运行,并定期浏览到由同一 Web 服务器 (IIS) 托管的页面以检查某些文本。如果发现某个条件成立,它将启动服务器(它所在的服务器)的重新启动。

我可以处理检查网页部分,这非常简单。但是重启之后呢?

有哪些 .NET 对象可以帮助解决这个问题,您能为我提供一些伪代码吗?我认为这应该非常简单,因为我猜测有一些内置的东西。

I want to write a program that will run on a specific web server and periodically browse to a page that is being hosted by that same web server (IIS) to check for certain text. If a certain condition was found to be true, it would initiate a reboot of the server (the one it is residing on).

I can handle the checking the webpage part, that's pretty simple. But what about the reboot?

What .NET objects are available that can help with this and can you throw down some pseudo code for me? I assume this should be pretty simple as I'm guessing there are things built in for this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流心雨 2024-09-25 19:16:24

只需使用 System.Diagnostic.Process 执行此命令即可

shutdown -r -f

Just execute this command with System.Diagnostic.Process

shutdown -r -f
°如果伤别离去 2024-09-25 19:16:15

我使用这段代码重新启动、注销和其他我的服务器......我希望这就是你想要的。

使用:

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);

    public const int SE_PRIVILEGE_ENABLED = 0x00000002;
    public const int TOKEN_QUERY = 0x00000008;
    public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
    public const int EWX_LOGOFF = 0x00000000;                               //Logoff flag   
    public const int EWX_SHUTDOWN = 0x00000001;                         //Shutdown
    public const int EWX_REBOOT = 0x00000002;                               //Reboot
    public const int EWX_FORCE = 0x00000004;                                //Force Logoff, shutdown or Reboot, add like this (EWX_REBOOT | EWX_FORCE)
    public const int EWX_POWEROFF = 0x00000008;                         //Forced shutdown
    public const int EWX_FORCEIFHUNG = 0x00000010;                      //Forced Logoff, shutdown or reboot if pc hangs


    public static 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);
    }

像这样重新启动:

DoExitWin(EWX_REBOOT);

如果您想强制重新启动,无论什么:

DoExitWin(EWX_REBOOT | EWX_FORCE);    

如果您只想在电脑挂起或花费很长时间时强制重新启动:

DoExitWin(EWX_REBOOT | EWX_FORCEIFHUNG);

I used this code to reboot, logoff and other my servers... I hope this is what you want.

Using:

using System.Runtime.InteropServices;

Code:

    [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);

    public const int SE_PRIVILEGE_ENABLED = 0x00000002;
    public const int TOKEN_QUERY = 0x00000008;
    public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
    public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
    public const int EWX_LOGOFF = 0x00000000;                               //Logoff flag   
    public const int EWX_SHUTDOWN = 0x00000001;                         //Shutdown
    public const int EWX_REBOOT = 0x00000002;                               //Reboot
    public const int EWX_FORCE = 0x00000004;                                //Force Logoff, shutdown or Reboot, add like this (EWX_REBOOT | EWX_FORCE)
    public const int EWX_POWEROFF = 0x00000008;                         //Forced shutdown
    public const int EWX_FORCEIFHUNG = 0x00000010;                      //Forced Logoff, shutdown or reboot if pc hangs


    public static 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);
    }

Reboot like so:

DoExitWin(EWX_REBOOT);

If you want to force reboot no meter what:

DoExitWin(EWX_REBOOT | EWX_FORCE);    

If you want to force reboot only if the pc hangs or it takes to long:

DoExitWin(EWX_REBOOT | EWX_FORCEIFHUNG);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文