问题
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 arguments. For some reason sysprep does not launch when code is run. It gives an error that file cannot be found. For e.g. by using the code below Notepad will open with no issues. If I try and open sysprep it will not.
Process.Start(@"C:\Windows\System32\notepad.exe"); -- opens with no issue
Process.Start(@"C:\Windows\System32\sysprep\sysprep.exe"); -- does not open
Any help would be appreciated.
{
public MainWindow()
{
InitializeComponent();
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
if (radioButtonYes.IsChecked == true)
{
Process.Start(@"C:\Windows\System32\sysprep\sysprep.exe");
}
}
回答1:
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.
回答2:
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.
回答3:
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();
来源:https://stackoverflow.com/questions/51869905/launch-sysprep-in-c-sharp