File.Exists returns false,Process.Start()无法启动文件,但它确实存在
File.Exists returns false,Process.Start() can't start the file,But it actually exists
注意:Windows我的计算机上启用了沙盒。
Console.WriteLine(File.Exists(@"C:\Windows\system32\WindowsSandbox.exe"));
Console.WriteLine(new FileInfo(@"C:\Windows\system32\WindowsSandbox.exe").Exists);
运行以上代码在C# Interactive
(VS不在admin模式):
True
True
但是,当我在控制台应用程序中 运行 它时,
(管理员模式和非管理员模式)。结果总是错误的。
False
False
我试过了Process.Start(@"C:\Windows\system32\WindowsSandbox.exe")
,
控制台应用程序失败(管理员和非管理员)但 C# 交互成功。
在 powershell 中,C:\Windows\system32\WindowsSandbox.exe
成功启动了 Windows 沙盒。
在资源管理器中(system32 文件夹):
image
谁能解释为什么会发生这种情况?
The Exists method returns false if any error occurs while trying to
determine if the specified file exists. This can occur in situations
that raise exceptions such as passing a file name with invalid
characters or too many characters, a failing or missing disk, or if
the caller does not have permission to read the file.
查看正在发生的事情的一种方法是尝试读取文件(例如使用 File.OpenRead)。如果成功,我会感到惊讶 - 但如果失败,异常应该为您提供更多信息。还可以从调用中检查 运行 命令(即 cmd whoami
等)
时使用的身份
我不熟悉 Windows 沙盒,但听起来这是一个架构问题。
在64位Windows上,C:\Windows\system32
是64位系统目录。您说的 Explorer 和 PowerShell 都可以看到 C:\Windows\system32\WindowsSandbox.exe
,将是 64 位进程,除非您特意使用 运行 32 位版本。
在 32 位应用程序中,路径 C:\Windows\system32
获取 redirected 到 32 位系统目录 C:\Windows\SysWOW64
。由于您说您没有 C:\Windows\SysWOW64\WindowsSandbox.exe
文件,您的应用程序也看不到(它认为的)C:\Windows\system32\WindowsSandbox.exe
文件,这表明您的应用程序是针对 32 位构建的。
所以,问题是 Windows Sandbox
没有 32 位版本供您 运行 的 32 位应用程序使用。当您将路径更改为 calc.exe
或 notepad.exe
时,您可以使用相同的代码,因为 Windows 提供了这些可执行文件的 32 位和 64 位版本。要让您的应用程序 运行 64 位 WindowsSandbox.exe
,您可以...
- 针对 64 位或更好的版本构建它...
- 让它执行路径
C:\Windows\sysnative\WindowsSandbox.exe
。 sysnative
是一个特殊的别名,允许 32 位应用程序在不重定向的情况下引用本机(64 位)系统目录。
至于为什么Visual Studio能看到C:\Windows\system32\WindowsSandbox.exe
,我也不好解释。这将取决于您使用的版本,但我的理解是仍然是 32 位应用程序,尽管某些组件是 64 位的是有道理的。至于诸如调试器之类的组件是否会 运行 与正在构建的 OS 或 application 的架构相同,我真的不知道。
注意:Windows我的计算机上启用了沙盒。
Console.WriteLine(File.Exists(@"C:\Windows\system32\WindowsSandbox.exe"));
Console.WriteLine(new FileInfo(@"C:\Windows\system32\WindowsSandbox.exe").Exists);
运行以上代码在C# Interactive
(VS不在admin模式):
True
True
但是,当我在控制台应用程序中 运行 它时, (管理员模式和非管理员模式)。结果总是错误的。
False
False
我试过了Process.Start(@"C:\Windows\system32\WindowsSandbox.exe")
,
控制台应用程序失败(管理员和非管理员)但 C# 交互成功。
在 powershell 中,C:\Windows\system32\WindowsSandbox.exe
成功启动了 Windows 沙盒。
在资源管理器中(system32 文件夹):
image
谁能解释为什么会发生这种情况?
The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.
查看正在发生的事情的一种方法是尝试读取文件(例如使用 File.OpenRead)。如果成功,我会感到惊讶 - 但如果失败,异常应该为您提供更多信息。还可以从调用中检查 运行 命令(即 cmd whoami
等)
我不熟悉 Windows 沙盒,但听起来这是一个架构问题。
在64位Windows上,C:\Windows\system32
是64位系统目录。您说的 Explorer 和 PowerShell 都可以看到 C:\Windows\system32\WindowsSandbox.exe
,将是 64 位进程,除非您特意使用 运行 32 位版本。
在 32 位应用程序中,路径 C:\Windows\system32
获取 redirected 到 32 位系统目录 C:\Windows\SysWOW64
。由于您说您没有 C:\Windows\SysWOW64\WindowsSandbox.exe
文件,您的应用程序也看不到(它认为的)C:\Windows\system32\WindowsSandbox.exe
文件,这表明您的应用程序是针对 32 位构建的。
所以,问题是 Windows Sandbox
没有 32 位版本供您 运行 的 32 位应用程序使用。当您将路径更改为 calc.exe
或 notepad.exe
时,您可以使用相同的代码,因为 Windows 提供了这些可执行文件的 32 位和 64 位版本。要让您的应用程序 运行 64 位 WindowsSandbox.exe
,您可以...
- 针对 64 位或更好的版本构建它...
- 让它执行路径
C:\Windows\sysnative\WindowsSandbox.exe
。sysnative
是一个特殊的别名,允许 32 位应用程序在不重定向的情况下引用本机(64 位)系统目录。
至于为什么Visual Studio能看到C:\Windows\system32\WindowsSandbox.exe
,我也不好解释。这将取决于您使用的版本,但我的理解是仍然是 32 位应用程序,尽管某些组件是 64 位的是有道理的。至于诸如调试器之类的组件是否会 运行 与正在构建的 OS 或 application 的架构相同,我真的不知道。