在C#中启动SYSPREP

时间:2018-08-16 05:04:10

标签: c# visual-studio launch process.start sysprep

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

Process.Start(@“ C:\ Windows \ System32 \ notepad.exe”); -打开没有问题 Process.Start(@“ C:\ Windows \ System32 \ sysprep \ sysprep.exe”); -无法打开

任何帮助将不胜感激。

{
    public MainWindow()
    {
        InitializeComponent();
    }

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

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

        }

    }

3 个答案:

答案 0 :(得分:1)

这实际上是在64位Windows上的重定向问题。 根据{{​​3}}, System32调用将重定向到SysWOW64文件夹。 而且由于C:\Windows\SysWOW64\Sysprep\sysprep.exe不存在,您会收到错误消息。

这就是您想要的:

Process p = Process.Start(@"C:\Windows\sysnative\Sysprep\sysprep.exe");

只需使用sysnative即可。

答案 1 :(得分:1)

我看到另一个答案对您有用,但是我想提供一个不同的答案,使您可以随时从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()”方法。希望这可以帮助其他人寻找此答案。

答案 2 :(得分:0)

我认为这是一个权限问题,您可以尝试以管理员身份运行

Process process = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.WindowStyle = ProcessWindowStyle.Hidden;
        startInfo.FileName ="cmd.exe";
        startInfo.Arguments = @"/c  C:\Windows\System32\sysprep\sysprep.exe";
        startInfo.Verb = "runas";
        process.StartInfo = startInfo;
        process.Start();