Why am I not getting a file not found by excpetion using the Interaction.Shell method?

I want to open a notepad file using the VisualBasic.Interaction.Shell method. I am currently getting an exception not found in the file using the following code.

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

But it works:

 int pid = Interaction.Shell(@"notepad.exe", AppWinStyle.NormalNoFocus, false, -1); 

That just opens the notepad file. Why is this?

I need to open the file in a specific location. I see some advantage when doing Interaction.Shell. How can I open a file in a specific location using Interaction.Shell?

+1
source share
1 answer

It seems that Interaction.Shell cannot open the application with a linked document. (a) the corresponding MSDN page does not say this (although the example for the PathName parameter seems erroneous then) and (b) even if D:\abc.txt exists, 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 .

Refresh If you really 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, since it usually provides more efficient processing of the running process. In this case, it also frees you from "knowing" which program is associated with .txt files (if you do not always want to use notepad.exe ).

+4
source

Source: https://habr.com/ru/post/906111/


All Articles