从 C# 程序启动 Sysprep.exe

Launch Sysprep.exe from C# Program

如何使用我的 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");

但是也不行(打开一个黑色window然后立即关闭)

运行 来自资源管理器的 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();

给出错误:

The system cannot find the file specified/nSystem.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at Windows_SSD_Optimizer_Method_1.Method1.btn_all_Click(Object sender, EventArgs e) in :line 182/n/nThe system cannot find the file specified/nSystem.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo) at System.Diagnostics.Process.Start() at Windows_SSD_Optimizer_Method_1.Method1.btn_all_Click(Object sender, EventArgs e) in :line 182/n/n

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

找到了 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);

在 Windows 8 x64 中你应该 be aware of File System Redirection

When a 32bit application wants to access the System32 folder on a 64bit OS, Windows 8 will default any paths to syswow64 on the idea that a 32bit program would really be looking for the 32bit files.

试试这个路径:

@"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();