[英]Launch SYSPREP in C#
我通过堆栈溢出搜索找到了这一点,但没有人给出有效的解决方案。 我正在编写一个简单的程序,它的第一部分是使用一些参数启动 sysprep.exe。 出于某种原因,sysprep 在代码运行时不会启动。 它给出了找不到文件的错误。 例如,通过使用下面的代码记事本将打开没有问题。 如果我尝试打开 sysprep,它不会。
Process.Start(@"C:\Windows\System32\notepad.exe"); -- opens with no issue
Process.Start(@"C:\Windows\System32\sysprep\sysprep.exe"); -- does not open
任何帮助,将不胜感激。
{
public MainWindow()
{
InitializeComponent();
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
if (radioButtonYes.IsChecked == true)
{
Process.Start(@"C:\Windows\System32\sysprep\sysprep.exe");
}
}
我看到另一个答案对您有用,但我想提供一个不同的答案,让您可以随时从 System32 访问文件。 如果您从一个公共类开始暂时修改内核,只要您拥有正确的权限,您应该能够访问您需要的任何内容。
public class Wow64Interop
{
[DllImport("Kernel32.Dll", EntryPoint = "Wow64EnableWow64FsRedirection")]
public static extern bool EnableWow64FSRedirection(bool enable);
}
在此之后,我写出我对 sysprep 的调用的方式如下
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);
}
}
执行此类操作时,您要确保该进程是否会重新启动您的电脑,不要使用“WaitForExit()”方法。 希望这可以帮助其他寻找此答案的人。
这实际上是 64 位 Windows 上的重定向问题。 根据这个讨论, System32
调用被重定向到SysWOW64
文件夹。 由于C:\\Windows\\SysWOW64\\Sysprep\\sysprep.exe
不存在,您会收到错误消息。
这就是你想要的:
Process p = Process.Start(@"C:\Windows\sysnative\Sysprep\sysprep.exe");
只需使用sysnative
代替。
我认为这是一个权限问题,您可以尝试以管理员身份运行
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.