如何在 Delphi 中运行命令行程序?
我需要从 Delphi 软件执行 Windows“查找”命令。我尝试使用 ShellExecute 命令,但它似乎不起作用。在 C 中,我会使用 system
过程,但在这里......我不知道。我想做这样的事情:
System('find "320" in.txt > out.txt');
编辑:感谢您的回答:) 我试图将“Find”作为可执行文件运行,而不是作为 cmd.exe 的参数运行。
I need to execute a Windows "find" command from a Delphi software. I've tried to use the ShellExecute
command, but it doesn't seem to work. In C, I'd use the system
procedure, but here... I don't know. I'd like to do something like this:
System('find "320" in.txt > out.txt');
Edit : Thanks for the answer :)
I was trying to run 'Find' as an executable, not as argument for cmd.exe.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
ShellExecute()
的示例:请注意,使用
CreateProcess()
而不是ShellExecute()
可以更好地控制进程。理想情况下,您还可以在辅助线程中调用它,并在进程句柄上调用
WaitForSingleObject()
以等待进程完成。示例中的Sleep()
只是一种等待ShellExecute()
启动的程序完成一段时间的方法 -ShellExecute()
不会那样做。如果确实如此,您无法简单地打开notepad
实例来编辑文件,ShellExecute()
将阻止您的父应用程序,直到编辑器关闭。An example using
ShellExecute()
:Note that using
CreateProcess()
instead ofShellExecute()
allows for much better control of the process.Ideally you would also call this in a secondary thread, and call
WaitForSingleObject()
on the process handle to wait for the process to complete. TheSleep()
in the example is just a hack to wait some time for the program started byShellExecute()
to finish -ShellExecute()
will not do that. If it did you couldn't for example simply open anotepad
instance for editing a file,ShellExecute()
would block your parent app until the editor was closed.变体 1(使用“高级”CreateProcess):
这将运行“DOS”程序并检索其输出:
变体 2:
在 [实时] 中捕获控制台输出以及如何在 TMemo 中获取它:
来源:delphi.wikia.com
Variant 1 (using the "advanced" CreateProcess):
This will run a 'DOS' program and retrieve its output:
Variant 2:
Capture console output in [Realtime] and how it in a TMemo:
Source: delphi.wikia.com