首页 > 解决方案 > 在 C# 中启动 SYSPREP

问题描述

我通过堆栈溢出搜索找到了这一点,但没有人给出有效的解决方案。我正在编写一个简单的程序,它的第一部分是使用一些参数启动 sysprep.exe。由于某种原因,运行代码时 sysprep 不会启动。它给出了找不到文件的错误。例如,通过使用下面的代码记事本将毫无问题地打开。如果我尝试打开 sysprep,它不会。

Process.Start(@"C:\Windows\System32\notepad.exe");  -- opens with no issue
Process.Start(@"C:\Windows\System32\sysprep\sysprep.exe");  -- does not open

任何帮助,将不胜感激。

{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void RadioButton_Checked(object sender, RoutedEventArgs e)
    {
        if (radioButtonYes.IsChecked == true)
        {

            Process.Start(@"C:\Windows\System32\sysprep\sysprep.exe");

        }

    }

标签: c#visual-studiolaunchprocess.startsysprep

解决方案


我看到另一个答案对你有用,但我想包括一个不同的答案,让你可以随时从 System32 访问文件。如果你从一个公共类开始暂时修改内核,只要你有正确的权限,你应该能够访问你需要的任何东西。

public class Wow64Interop
    {
        [DllImport("Kernel32.Dll", EntryPoint = "Wow64EnableWow64FsRedirection")]
        public static extern bool EnableWow64FSRedirection(bool enable);
    } 

在此之后,我写出我对 sysprep 的调用的方式如下

private void RunSysprep()
    {
        try
        {
            if (Wow64Interop.EnableWow64FSRedirection(true) == true)
            {
                Wow64Interop.EnableWow64FSRedirection(false);
            }

            Process Sysprep = new Process();
            Sysprep.StartInfo.FileName = "C:\\Windows\\System32\\Sysprep\\sysprep.exe";
            Sysprep.StartInfo.Arguments = "/generalize /oobe /shutdown /unattend:\"C:\\Windows\\System32\\Sysprep\\unattend.xml\"";
            Sysprep.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
            Sysprep.Start();

            if (Wow64Interop.EnableWow64FSRedirection(false) == true)
            {
                Wow64Interop.EnableWow64FSRedirection(true);
            }

        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

在执行此类操作时,您要确保该过程是否会重新启动您的电脑以不使用“WaitForExit()”方法。希望这可以帮助其他寻找此答案的人。


推荐阅读