1
votes

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

http://i.stack.imgur.com/0Em8O.png

2
Why don't you just start the sysprep.exe? There is no need to run cmd.exeSami Kuhmonen
but how do I pass the arguments /audit /reboot ?kks21199
cmd.StartInfo.ArgumentsSami Kuhmonen
I tried starting sysprep.exe with process but it doesn't work :/kks21199
Is your program running as 32 bit on a 64 bit system, if so you are looking in the wrong system folder.Scott Chamberlain

2 Answers

0
votes

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

Found a solution for Windows Versions older than 8:

You need to compile your programm as x64 binary. If you compile it as x86 binary the system will redirect you to SysWOW64 instead of System32.

If you need to be compatible with x64 and x86 you could also disable folder redirection:

 // Disables folder redirection
 [DllImport("kernel32.dll", SetLastError = true)]
 static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

 // Enables folder redirection
 [DllImport("kernel32.dll", SetLastError = true)]
 static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);