批处理文件-从c#程序启动Sysprep.exe

本文关键字:启动 Sysprep exe 程序 批处理文件 | 更新日期: 2023-09-27 17:49:42

如何在c#程序中使用特定参数启动sysprep.exe ?

 public void cmdPrint(string[] strcommmand)
    {
        Process cmd = new Process();
        cmd.StartInfo.FileName = "cmd.exe";
        cmd.StartInfo.RedirectStandardInput = true;
        cmd.StartInfo.RedirectStandardOutput = true;
        cmd.StartInfo.CreateNoWindow = true;
        cmd.StartInfo.UseShellExecute = false;
        cmd.Start();
        cmd.StandardInput.WriteLine("cd c:''");
        foreach (string str in strcommmand)
        {
            cmd.StandardInput.WriteLine(str);
        }
        cmd.StandardInput.Flush();
        cmd.StandardInput.Close();
        writeLine(cmd.StandardOutput.ReadToEnd());
    }

我在Windows窗体应用程序中调用它,

string[] cmd = { "cd C:''Windows''System32''Sysprep", "sysprep.exe /audit /reboot"};consoleBox1.cmdPrint(cmd);

但是它似乎没有启动sysprep.exe。我将这两个命令粘贴到.bat文件中,并使用

命令启动它。

System.Diagnostics.Process.Start(Application.StartupPath + "''awesome.bat");

但它也不工作(打开一个黑色窗口并立即关闭)

从资源管理器中运行bat文件,所以我想我在c#应用程序中缺少一些权限。

在我的app.manifest中

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

是否可以启动sysprep ?我的应用程序可以在Windows 7、8、8.1和10的正常桌面和审计模式下运行。

编辑:

我尝试了没有完成和关闭cmd的代码,但程序没有响应

var procInfo = new 
ProcessStartInfo("C:''Windows''System32''Sysprep''sysprep.exe");
                procInfo.Arguments = "/audit /reboot";
                var proc = new Process();
                proc.StartInfo = procInfo;
                proc.Start(); //Actually executes the process
                proc.WaitForExit();

给出错误:

系统找不到文件指定/nSystem.ComponentModel。Win32Exception (0x80004005):告警解释系统找不到指定的文件System.Diagnostics.Process.StartWithShellExecuteEx (ProcessStartInfostart () . start ()Windows_SSD_Optimizer_Method_1.Method1。btn_all_Click(对象发送者,事件参数e) in:line 182/n/n系统找不到文件/nSystem.ComponentModel指定。Win32Exception (0x80004005):告警解释系统找不到指定的文件System.Diagnostics.Process.StartWithShellExecuteEx (ProcessStartInfostart () . start ()Windows_SSD_Optimizer_Method_1.Method1。btn_all_Click(对象发送者,EventArgs e) in:第182行/n/n

https://i.stack.imgur.com/0Em8O.png

批处理文件-从c#程序启动Sysprep.exe

在Windows 8 x64中,您应该了解文件系统重定向。

当一个32位应用程序想要访问System32文件夹时64位操作系统,Windows 8将默认任何路径为syswow64一个32位的程序会查找32位的文件。

试试这个路径:

@"c:'windows'sysnative'sysprep'sysprep.exe"

var procInfo = new ProcessStartInfo(@"c:'windows'sysnative'sysprep'sysprep.exe");
procInfo.Arguments = "/audit /reboot";
var proc = new Process();
proc.StartInfo = procInfo;
proc.Start(); //Actually executes the process
proc.WaitForExit();

找到Windows版本低于8的解决方案:

您需要将程序编译为x64二进制文件。如果你把它编译成x86二进制文件,系统会把你重定向到SysWOW64而不是System32

如果你需要兼容x64和x86,你也可以禁用文件夹重定向:

 // Disables folder redirection
 [DllImport("kernel32.dll", SetLastError = true)]
 static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);
 // Enables folder redirection
 [DllImport("kernel32.dll", SetLastError = true)]
 static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);