如何从 Process.Start 抛出 FileNotFoundException
How to Throw FileNotFoundException from Process.Start
我正在尝试对可执行文件调用 Process.Start()
。如果找不到该文件,请将其复制到所需位置,然后重试。
根据the documentation,当The file specified in the startInfo parameter's FileName property could not be found.
时,Process.Start()
可以抛出一个FileNotFoundException
基于此,以下似乎是一种合理的方法:
try
{
Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
}
catch (FileNotFoundException ex)
{
File.Copy(@"Z:\Unused\Apps\IT Support App\IT Self Help.exe", @"C:\users\Angus.McAngerson\desktop");
Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe", "vdi");
}
但是,try
块中的 Start()
只会抛出 Win32Exception
:
An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
Message: The system cannot find the file specified
ErrorCode: -2147467259
NativeErrorCode: 2
我尝试将 try
代码更改为:
var procsi = new ProcessStartInfo(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
Process.Start(procsi);
但结果相同。我还尝试将 BuildPlatform 更改为 x86
、x6
和 Any CPU
,但没有任何区别。
为什么会这样?我怎样才能抛出 FileNotFoundException
?
更新
文档指出:
FileNotFoundException:
The file specified in the startInfo parameter's FileName property could not be found.
在上述情况下,无法找到文件,但代码会引发不同的异常。如果不是完全不真实,那至少是非常具有误导性的。
我能想到的唯一解释是程序试图 运行 文件而不检查它是否存在。这很公平,但是 FileNotFoundException
会在什么情况下发生?
这是文档中的错误吗?
如果您查看 Process.Start 的文档,您会注意到有 table 的异常情况,包括:
Win32Exception - 打开关联文件时出错。
FileNotFoundException - PATH 环境变量有一个包含引号的字符串。
看起来它对我来说是正确的,即使异常有点误导
如果你看这里:https://msdn.microsoft.com/en-us/library/system.componentmodel.win32exception(v=vs.110).aspx
它会告诉你,当你试图打开一个不存在的可执行文件时,你会得到 Win32Exception。
实际上,您的代码似乎运行良好。
但是如果你真的想要一个 FileNotFoundException,那么你将不得不在 运行 处理之前自己做一些检查并自己抛出异常。
if(!File.Exists(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe"))
{
throw new FileNotFoundException("This file was not found.");
}
编辑
似乎文档中可能存在错误,因为我也无法让它抛出 FileNotFoundException
,即使它声称能够。
因此,您可以处理 Win32Exception 或执行我上面建议的操作。
也许其他人可以阐明这一点?
除非与异常跟踪相关,否则在catch中执行语句确实不是一个好主意。
我不明白你为什么不能使用 File.Exists(因为你打算对 FileNotFoundException 不做任何事情)。如果我们重写您的代码,那么它将是:
if (File.Exists(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe"))
{
Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
}
else
{
File.Copy(@"Z:\Unused\Apps\IT Support App\IT Self Help.exe", @"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe", "vdi");
}
我正在尝试对可执行文件调用 Process.Start()
。如果找不到该文件,请将其复制到所需位置,然后重试。
根据the documentation,当The file specified in the startInfo parameter's FileName property could not be found.
Process.Start()
可以抛出一个FileNotFoundException
基于此,以下似乎是一种合理的方法:
try
{
Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
}
catch (FileNotFoundException ex)
{
File.Copy(@"Z:\Unused\Apps\IT Support App\IT Self Help.exe", @"C:\users\Angus.McAngerson\desktop");
Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe", "vdi");
}
但是,try
块中的 Start()
只会抛出 Win32Exception
:
An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
Message: The system cannot find the file specified
ErrorCode: -2147467259
NativeErrorCode: 2
我尝试将 try
代码更改为:
var procsi = new ProcessStartInfo(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
Process.Start(procsi);
但结果相同。我还尝试将 BuildPlatform 更改为 x86
、x6
和 Any CPU
,但没有任何区别。
为什么会这样?我怎样才能抛出 FileNotFoundException
?
更新
文档指出:
FileNotFoundException:
The file specified in the startInfo parameter's FileName property could not be found.
在上述情况下,无法找到文件,但代码会引发不同的异常。如果不是完全不真实,那至少是非常具有误导性的。
我能想到的唯一解释是程序试图 运行 文件而不检查它是否存在。这很公平,但是 FileNotFoundException
会在什么情况下发生?
这是文档中的错误吗?
如果您查看 Process.Start 的文档,您会注意到有 table 的异常情况,包括:
Win32Exception - 打开关联文件时出错。
FileNotFoundException - PATH 环境变量有一个包含引号的字符串。
看起来它对我来说是正确的,即使异常有点误导
如果你看这里:https://msdn.microsoft.com/en-us/library/system.componentmodel.win32exception(v=vs.110).aspx
它会告诉你,当你试图打开一个不存在的可执行文件时,你会得到 Win32Exception。
实际上,您的代码似乎运行良好。
但是如果你真的想要一个 FileNotFoundException,那么你将不得不在 运行 处理之前自己做一些检查并自己抛出异常。
if(!File.Exists(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe"))
{
throw new FileNotFoundException("This file was not found.");
}
编辑
似乎文档中可能存在错误,因为我也无法让它抛出 FileNotFoundException
,即使它声称能够。
因此,您可以处理 Win32Exception 或执行我上面建议的操作。
也许其他人可以阐明这一点?
除非与异常跟踪相关,否则在catch中执行语句确实不是一个好主意。
我不明白你为什么不能使用 File.Exists(因为你打算对 FileNotFoundException 不做任何事情)。如果我们重写您的代码,那么它将是:
if (File.Exists(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe"))
{
Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
}
else
{
File.Copy(@"Z:\Unused\Apps\IT Support App\IT Self Help.exe", @"C:\users\Angus.McAngerson\desktop\IT Self Help.exe");
Process.Start(@"C:\users\Angus.McAngerson\desktop\IT Self Help.exe", "vdi");
}