进程和可执行文件名

发布于 2024-11-24 12:03:34 字数 421 浏览 1 评论 0原文

C# Process 类允许您在 Windows 中运行命令行可执行文件。 Process.StartInfo 类保存有关要运行的命令行作业的信息,包括其路径和文件名。

我的问题是,如果文件不存在, Process.Start() 将引发异常。为了尝试避免该异常,我编写了一个检查来查看该文件是否存在,但这在所有情况下都不能正常工作,因为名称可能不作为具有该确切名称的文件存在,但可能有一个可执行的对应项。例如,“C:\SoAndSo”可能不作为文件存在,但“C:\SoAndSo*.exe*”存在,并且在启动 Process 命令时会成功找到

我已经破解了我的检查以尝试给我的名称,以及名称+“.exe”和“.bat”,但这感觉很笨拙,也许我缺少其他可执行扩展。

那么:是否有一个命令可以询问以下问题:“您是否有以“SoAndSo”开头的文件名的可执行版本?

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 exception 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'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沙与沫 2024-12-01 12:03:34

为什么不尝试启动 Process 并将其包装在 try/catch 中,当您收到 FileNotFoundException 时,您可以简单地打印其输出并退出?我看到您想尝试避免异常,但尝试阻止异常可能比仅仅引发异常要花费更多的开销。

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.

栀子花开つ 2024-12-01 12:03:34

id 有类似的要求.. &我使用环境变量%PATHEXT%解决了这个问题。

可以在此处找到参考,
假设我正在尝试查找一个名为 abcd 的程序,那么应用程序将查找

*abc*. 以及 %PATHEXT% 变量中可用的所有扩展组合。

我'我想说这是一个粗略的实现..但它对我有用..我很确定有更好的方法来做到这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文