Names of processes and executables

The C # Process class allows you to run the command line executable on Windows. The Process.StartInfo class contains information about the command line job that you want to run, including its path and file name.

My problem is that Process.Start () will throw an exception if the file does not exist. To avoid an exception, I encoded the check to check if the file exists, but in all cases it does not work properly, as the name may not exist as a file with that exact name, but it may have an executable instance. For example, "C: \ SoAndSo" may not exist as a file, but "C: \ AAndSo * .exe *" does and will be successfully found when you run the Process command.

I hacked my checks to try the name given to me, and the name + ".exe" and ".bat", but this seems awkward, and maybe I am missing other executable extensions.

So: is there a command to ask the question: "Do you have an executable version of the file name starting with" SoAndSo "?

+4
source share
2 answers

id had a similar requirement .. and I decided to use the environment variable %PATHEXT% .

the link can be found here , let's say I'm trying to find a program called abcd, then the application will search

*abc*. with all available extensions available for the %PATHEXT% variable.

I would say that this is a rough implementation. But it worked for me. I am sure there is a better way to do this.

+2
source

Why not try running Process and wrap this in a try / catch, and when you get a FileNotFoundException, you can just print the output of this and exit? I see that you want to try to throw an exception, but you may have more overhead to prevent this than just bear it.

+3
source

All Articles