开发者

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

开发者 https://www.devze.com 2023-03-31 23:32 出处:网络
I know file operations are rather non-standard between OS\'s and API\'s but I would like to discover whether an execu开发者_开发技巧table (named at run time) exists in the path.

I know file operations are rather non-standard between OS's and API's but I would like to discover whether an execu开发者_开发技巧table (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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消