1

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.stack.imgur.com/0Em8O.png

Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105
kks21199
  • 1,116
  • 2
  • 10
  • 29

2 Answers2

0

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);
davidgiga1993
  • 2,695
  • 18
  • 30
  • highestAvailable might not run as administrator if the user has no admin rights. requireAdministrator forces to get administrator rights. – davidgiga1993 May 19 '15 at 05:55
  • 1
    Tried it, but it still says file not found. Tried running it through VS, `An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll` `Additional information: The system cannot find the file specified` My UAC is already set to the lowest in windows – kks21199 May 19 '15 at 05:57
  • got the same exception here, thought this would solve the problem. I'll take a look a further look at it. – davidgiga1993 May 19 '15 at 05:57
0

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();
Vojtěch Dohnal
  • 7,867
  • 3
  • 43
  • 105