c# - 在 C# 中启动 SYSPREP
问题描述
我通过堆栈溢出搜索找到了这一点,但没有人给出有效的解决方案。我正在编写一个简单的程序,它的第一部分是使用一些参数启动 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()”方法。希望这可以帮助其他寻找此答案的人。
推荐阅读
- python-2.7 - 为什么我的 VPython 程序在这个 Glowscript IDE 上运行如此缓慢?
- database - Spring Data JPA/Hibernate 中锁的范围是什么?
- python - Python - 在单元测试中返回异常
- c# - .NET 内存中的浮点表示
- java - 使用 hh:mm:ss 每天在特定时间运行的计划
- arrays - 在SAS中按组填充先前观察中的许多变量的缺失值
- php - 在 Laravel 5.1 中上传多张图片
- vba - 循环收集标签和计算
- python - 在python中组合来自两个不同字符串的数据
- python - Python3 - 处理库依赖于操作系统的情况