The C# Process class allows you to run a command-line executable in Windows. The Process.StartInfo class holds information about the command line job you want to run, including its path and filename.
My problem is that Process.Start() will throw an exc开发者_如何学Pythoneption if the file does not exist. To try and avoid the exception, I coded a check to see if the file existed, but this did not work properly in all instances because a name might not exist as a file with that exact name, but might have an executable counterpart. For example, "C:\SoAndSo" might not exist as a file, but "C:\SoAndSo*.exe*" does, and would have been found successfully when starting the Process command.
I have hacked my checks to try the name given to me, and the name + ".exe" and ".bat", but this feels cludgy, and perhaps I am missing other executable extensions.
So: is there a command to ask the question: 'Do you have an executable version of the filename starting 'SoAndSo'?
Why not try to start the Process and wrap that in a try/catch and when you get a FileNotFoundException then you can simply print the output of that and exit? I see you want to try and avoid the exception but you might have more overhead trying to prevent it than just incurring it.
id had a similar requirment.. & i solved it using the environment variable %PATHEXT%
.
reference can be found here, say I'm trying to find a program called abcd then the app would look for
*abc*.
with all extension combo available ion the %PATHEXT%
variable..
I'd say this is a crude implementation.. but it worked for me.. I;m quite sure there is a better way to do this.
精彩评论