Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从托管代码启动Windows沙盒_C#_Windows Sandbox - Fatal编程技术网

C# 从托管代码启动Windows沙盒

C# 从托管代码启动Windows沙盒,c#,windows-sandbox,C#,Windows Sandbox,我正试图以编程方式初始化。 我的目标是生成一个.wsb配置文件,然后启动它。 目前我使用的是Process.Start()方法,但我一直在Start()方法上遇到错误 这是代码: var sbProc = new Process(); sbProc.StartInfo.FileName = configPath; sbProc.Start(); sbProc.WaitForExit(); sbProc.StartInfo.UseShellExecute = false; 抛出:System.

我正试图以编程方式初始化。 我的目标是生成一个
.wsb
配置文件,然后启动它。 目前我使用的是
Process.Start()
方法,但我一直在
Start()
方法上遇到错误

这是代码:

var sbProc = new Process();
sbProc.StartInfo.FileName = configPath;
sbProc.Start();
sbProc.WaitForExit();
sbProc.StartInfo.UseShellExecute = false;
抛出:
System.ComponentModel.Win32Exception:“未找到应用程序”

我确信该文件存在,因为我已尝试双击并通过命令行打开它。两者都像预期的那样工作,所以我假设它是指关联的应用程序(在本例中为Windows沙盒)

我尝试添加:

var sbProc = new Process();
sbProc.StartInfo.FileName = configPath;
sbProc.Start();
sbProc.WaitForExit();
sbProc.StartInfo.UseShellExecute = false;
抛出:
System.ComponentModel.Win32Exception:“指定的可执行文件不是此操作系统平台的有效应用程序。”

这是相同的例外,但不同的消息,这实际上是非常混乱的。如上所述,我100%确信我的操作系统支持此功能;支持并启用每个需求。
Process.Start()
是否可能无法处理
.wsb
文件?如果是这样,我如何才能实现我想要的

我愿意接受任何建议,并提前表示感谢

更新:

var sbProc = new Process();
sbProc.StartInfo.FileName = configPath;
sbProc.Start();
sbProc.WaitForExit();
sbProc.StartInfo.UseShellExecute = false;
我已尝试使用以下代码将动词更改为
Invoke
Start

sbProc.StartInfo.Verb = "Start";

抛出:
System.ComponentModel.Win32Exception:“没有应用程序与此操作的指定文件关联”


如何将文件与应用程序关联?

Windows沙盒通过在资源管理器中双击并通过
cmd.exe/c start
工作,但无法在c应用程序中以编程方式启动。原因是C#应用程序是32位的,而操作系统是64位的

当32位应用程序尝试调用沙盒配置时,它(间接)尝试调用
%windir%\System32\WindowsSandbox.exe
可执行文件。但是当32位应用程序尝试在包含32位版本的系统应用程序和DLL的64位Windows上访问
%windir%\System32
时。由于没有32位
WindowsSandbox.exe
,.NET framework引发异常
System.ComponentModel.Win32异常:“未找到应用程序”

有两种方法可以从32位用户应用程序启动64位系统应用程序

计划A 使用
%windir%\Sysnative
路径。它仅适用于32位应用程序,并解析为真正的64位
System32
system文件夹

var sbProc = new Process();
sbProc.StartInfo.FileName = "%SystemRoot%\Sysnative\WindowsSandbox.exe";
sbProc.StartInfo.Arguments = "\"" +configPath + "\"";
sbProc.StartInfo.UseShellExecute = true;
sbProc.Start();
sbProc.WaitForExit();
我猜,在这种情况下,
WaitForExit
将等待沙箱完成。我不是100%确定,Windows中可能会发生一些事情,但请期待

方案B 计划A有以下风险:如果将来MS人员重命名沙盒的可执行文件或添加额外的CLI开关,直接调用可能会失败。你必须修改你的代码

为了避免这种情况,您可以使用
cmd.exe
通过
start
命令启动沙盒
cmd.exe
可以在后台调用64位系统应用程序

var sbProc = new Process();
sbProc.StartInfo.FileName = "cmd";
sbProc.StartInfo.Arguments = "/c start \"\" \"" +configPath + "\"";
sbProc.StartInfo.Start();
sbProc.WaitForExit();

唯一的问题是,
WaitForExit
将在
start
完成后尽快返回,而不是沙箱<代码>启动不会等待启动的进程。

在Windows上启动新进程有两种方法。首先,让内核直接启动它。第二是要求壳牌启动它。我想您需要第二种方法,因为它似乎需要一个shell来准备一些东西。我是否使用
StartInfo.UseShellExecute=true
来实现这一点?如果是这样,
true
是默认值。我不确定.NET是否正确,但在Win32中它使用
ShellExecute
。您可能需要将
动词
更改为
打开
以外的内容。谢谢您的提示。我会调查的that@AsPas,在HCR\.wsb下应该有一个文件类型的密钥,类似于“ms sandbox”。open命令是为文件类型而不是扩展名定义的:例如HKEY\U CLASSES\U ROOT\ms sandbox\Shell\open\command这是我当前的解决方案。但正如您所说,我不能等待沙盒进程完成,这是有问题的。@AsPas,我添加了“计划A”来直接调用
windowsandbox.exe
,我已经尝试过了,但仍然遇到异常。这次:
System.ComponentModel.Win32Exception:“系统找不到指定的文件”
。这是巫术…@AsPas,你的应用程序是32位的。您应该将其配置为64位或使用以下路径:
%windir%\Sysnative\WindowsSandbox.exe
。对于32位应用程序,虚拟目录
%windir%\Sysnative
指向64位
System32
目录,无需重定向。这解决了我的问题。它确实试图在WOW64文件夹中找到32位版本的沙盒,但该文件夹并不存在。请更新您的答案以包含此信息和Microsoft页面的链接。非常感谢您的帮助和耐心!!