Search code examples
c#batch-filewindows-864-bitsysprep

Launch Sysprep.exe from C# Program


How could I launch sysprep.exe with specific arguments from my c# program ?

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

    }

and I call it from my Windows Form Application,

string[] cmd = { "cd C:\\Windows\\System32\\Sysprep", "sysprep.exe /audit /reboot"}; consoleBox1.cmdPrint(cmd);

But it doesn't seem to start the sysprep.exe. I pasted the two commands in a .bat and launched it with,

System.Diagnostics.Process.Start(Application.StartupPath + "\\awesome.bat");

but it doesn't work either (opens a black window and closes immediately)

Running the bat file from explorer works, so i guess I am missing some permission in my c# application.

In my app.manifest,

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

Is it possible to launch sysprep ? My application is made to run on Windows 7,8,8.1 and 10 on the normal desktop and on audit mode.

EDIT:

I tried the code without fulshing and closing the cmd but the program went to not responding

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

Gives error :

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

https://i.sstatic.net/0Em8O.png


Solution

  • In Windows 8 x64 you should 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.

    Try this path instead:

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