为什么使用 Interaction.Shell 方法会出现文件未找到的异常?

发布于 2024-12-27 18:52:36 字数 437 浏览 5 评论 0原文

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫人生路 2025-01-03 18:52:36

看起来好像 Interaction.Shell 无法通过关联文档打开应用程序。 (a) 相关的 MSDN 页面 并没有这么说(尽管 PathName 参数的示例似乎有误导性)和 (b) 即使 D:\abc.txt 确实存在, 它失败。

或者,您可以使用 System.Diagnostics.Process类:

using (Process process = Process.Start(@"D:\abc.txt"))
{
    int pid = process.Id;

    // Whether you want for it to exit, depends on your needs. Your
    // Interaction.Shell() call above suggests you don't.  But then
    // you need to be aware that "pid" might not be valid when you
    // you look at it, because the process may already be gone.
    // A problem that would also arise with Interaction.Shell.
    // process.WaitForExit();
}

请注意,D:\abc.txt 必须存在,否则您仍然会收到 FileNotFoundException

更新如果您确实需要使用Interaction.Shell,您可以使用以下内容就

int pid = Interaction.Shell(@"notepad.exe D:\abc.txt", false, -1);

我个人而言,我会使用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 the PathName parameter seems missleading then) and (b) even if D:\abc.txt does exist, it fails.

Alternatively you can use the System.Diagnostics.Process class:

using (Process process = Process.Start(@"D:\abc.txt"))
{
    int pid = process.Id;

    // Whether you want for it to exit, depends on your needs. Your
    // Interaction.Shell() call above suggests you don't.  But then
    // you need to be aware that "pid" might not be valid when you
    // you look at it, because the process may already be gone.
    // A problem that would also arise with Interaction.Shell.
    // process.WaitForExit();
}

Note that D:\abc.txt must exist, or you still get a FileNotFoundException.

Update If you really do need to use Interaction.Shell, you can use the following

int pid = Interaction.Shell(@"notepad.exe D:\abc.txt", false, -1);

Personally, 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 use notepad.exe).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文