Launch SYSPREP in C#

后端 未结 3 1599
南旧
南旧 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: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.

提交回复
热议问题