简体   繁体   中英

How to close command prompt?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication4
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            var psi = new ProcessStartInfo();

            psi.UseShellExecute = true;
            psi.Verb = "runas";
            psi.FileName = @"C:\Windows\System32\cmd.exe";

            psi.Arguments = "/env /user:" + "Administrator" + @" %windir%\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f";

            Process.Start(psi);
            System.Diagnostics.Process.Start(System.Windows.Forms.Application.StartupPath + "\\DllRegister.bat");
            /// need to close the command prompt over here ????????????????

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

使用命令行参数 /c而不是/k来执行命令,然后终止命令shell。

Try /C modifier instead of /K

psi.Arguments = "/env /user:" + "Administrator" + @" %windir%\System32\cmd.exe /C %windir%\System32\reg.exe ADD HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f";

The diffrence between these two is as follows

/C  Carries out the command specified by string and then terminates
/K  Carries out the command specified by string but remains

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM