John
John

Reputation: 6648

How to find whether an executable is in the PATH without iterating all the folders on the path (or executing it)

I know file operations are rather non-standard between OS's and API's but I would like to discover whether an executable (named at run time) exists in the path.

This is a validation of user input and later on the app is called using

        ProcessStartInfo ^processStartInfo = gcnew ProcessStartInfo("ReallyCool.exe");
        Process ^process = gcnew Process();
        process->StartInfo = processStartInfo;
        bool processStarted = process->Start();

this throws only when it comes to the last line above.

Does anyone know how I can verify the file is there before taking the risk of actually executing it or am I being a naif by allowing users to run '.exes' through my app?

As per title, I am keen to avoid searching every directory on the path explicitly if possible. Nor am I certain how I could obtain the Windows path from .NET coding.

Upvotes: 1

Views: 102

Answers (1)

Dmitry Ornatsky
Dmitry Ornatsky

Reputation: 2237

To check if the file is there, use bool doesFileExist = System.IO.File.Exists(path);

I doubt there is an easy way to know if it is a valid executable without trying to run it.

Upvotes: 1

Related Questions