为什么使用 Interaction.Shell 方法会出现文件未找到的异常?
我想使用 VisualBasic.Interaction.Shell
方法打开记事本文件。目前,我使用以下代码得到文件未找到异常。
int pid = Interaction.Shell(@"D:\abc.txt", AppWinStyle.NormalNoFocus, false, -1);
但这是有效的:
int pid = Interaction.Shell(@"notepad.exe", AppWinStyle.NormalNoFocus, false, -1);
它只会打开一个记事本文件。这是为什么呢?
我确实需要它来打开特定位置的文件。我看到了 Interaction.Shell 执行的一些优势。如何使用 Interaction.Shell 打开特定位置的文件?
I want to open a notepad file using VisualBasic.Interaction.Shell
method. At present I get a file not found exception using the following code.
int pid = Interaction.Shell(@"D:\abc.txt", AppWinStyle.NormalNoFocus, false, -1);
But this works:
int pid = Interaction.Shell(@"notepad.exe", AppWinStyle.NormalNoFocus, false, -1);
Which just opens a notepad file. Why is this?
I do need it to open a file in a specific location. I see some advantage with Interaction.Shell execution. How is it possible to open a file in a specific location using Interaction.Shell?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来好像
Interaction.Shell
无法通过关联文档打开应用程序。 (a) 相关的 MSDN 页面 并没有这么说(尽管PathName
参数的示例似乎有误导性)和 (b) 即使D:\abc.txt
确实存在, 它失败。或者,您可以使用
System.Diagnostics.Process
类:
请注意,
D:\abc.txt
必须存在,否则您仍然会收到FileNotFoundException
。更新如果您确实需要使用
Interaction.Shell
,您可以使用以下内容就我个人而言,我会使用
Process
类,因为它通常为启动的进程提供更稳健的处理。在这种情况下,它还使您无需“了解”哪个程序与.txt
文件关联(除非您总是想使用notepad.exe
)。It looks as if
Interaction.Shell
cannot open an application by an associated document. (a) the relevant MSDN page does not say so (although the example for thePathName
parameter seems missleading then) and (b) even ifD:\abc.txt
does exist, it fails.Alternatively you can use the
System.Diagnostics.Process
class:Note that
D:\abc.txt
must exist, or you still get aFileNotFoundException
.Update If you really do need to use
Interaction.Shell
, you can use the followingPersonally, I would go with the
Process
class, as it generally provides more robus handling of the launched process. In this case it also frees you from "knowing" which program is associated with.txt
files (unless you always want to usenotepad.exe
).