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?
It looks as if
Interaction.Shellcannot open an application by an associated document. (a) the relevant MSDN page does not say so (although the example for thePathNameparameter seems missleading then) and (b) even ifD:\abc.txtdoes exist, it fails.Alternatively you can use the
System.Diagnostics.Processclass:Note that
D:\abc.txtmust 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
Processclass, 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.txtfiles (unless you always want to usenotepad.exe).