Launch SYSPREP in C#

后端 未结 3 1578
南旧
南旧 2021-01-26 05:00

I have found this via stack-overflow search but no one has given a solution that works. I am writing a simple program and the first part of it is to launch sysprep.exe with some

相关标签:
3条回答
  • 2021-01-26 05:29

    It is actually a redirection problem on 64 bit Windows. According to this discussion, the System32 calls are redirected to the SysWOW64 folder. And since C:\Windows\SysWOW64\Sysprep\sysprep.exe does not exist, you get the error.

    This is what you want:

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

    Simply use sysnative instead.

    0 讨论(0)
  • 2021-01-26 05:32

    I think this is a permission problem, you can try run as admin

    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();
    
    0 讨论(0)
  • 2021-01-26 05:55

    I see that another answer has worked for you, but I would like to include a different answer that will allow you to access files from System32 at any time. If you start with a public class to modify the kernel momentarily you should be able to access anything you need so long as you have the right permissions.

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

    After this the way that i wrote out my call to sysprep was as follows

    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);
            }
        }
    

    when doing something like this you want to make sure that if the process will restart your pc to NOT use the "WaitForExit()" method. Hope this helps anyone else looking for this answer.

    0 讨论(0)
提交回复
热议问题