怎样在Delpi中调用一个Dos命令并将结果输出到一个TMemo中?

螃蟹就是横着走 2002-11-21 11:59:49
以前看过一篇相关文章,但找不到了,希望高人指点一二!
...全文
92 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
goomoo 2002-11-21
  • 打赏
  • 举报
回复
以下是c:\test.bat的内容:

rem--------------------
@echo off
dir c:\>c:\test.txt
rem--------------------

分数太少。
goomoo 2002-11-21
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
begin
winExec('c:\test.bat',0);

end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
if (fileExists('c:\test.txt')) then
memo1.Lines.LoadFromFile('c:\test.txt');
end;
  • 打赏
  • 举报
回复
unit Console;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,ShellAPI;

type
TConsoleForm = class(TForm)
btOpen: TButton;
btClose: TButton;
btRun: TButton;
btOutput: TButton;
procedure btOpenClick(Sender: TObject);
procedure btCloseClick(Sender: TObject);
procedure btOutputClick(Sender: TObject);
procedure btRunClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
ConsoleForm: TConsoleForm;

implementation

{$R *.dfm}

procedure TConsoleForm.btOpenClick(Sender: TObject);
begin
if not AllocConsole then Application.MessageBox('Can''t allocate console!','Console',MB_OK OR MB_ICONINFORMATION)
else SetConsoleTitle('Console Demo');
end;

procedure TConsoleForm.btCloseClick(Sender: TObject);
begin
if not FreeConsole then Application.MessageBox('Can''t free console!','Console',MB_OK OR MB_ICONINFORMATION);
end;

procedure TConsoleForm.btOutputClick(Sender: TObject);
var OutText:PChar;nWrite:Cardinal;sHandle:Cardinal;
begin
OutText:='Hello,console!'#$A#$D;
sHandle:=GetStdHandle(STD_OUTPUT_HANDLE);
if (sHandle<>0) then
begin
WriteConsole(sHandle,OutText,StrLen(OutText),nWrite,nil);
end;
end;

procedure TConsoleForm.btRunClick(Sender: TObject);
begin
//这里要运行dos的dir命令,并将结果输出到"Open"按钮打开的Console
//Window,怎么写
end;

end.
hansion3406 2002-11-21
  • 打赏
  • 举报
回复
还是楼上的比较安全..最好用Shellexecute
jackie168 2002-11-21
  • 打赏
  • 举报
回复
uses Shellapi;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShellExecute(handle,nil,pchar('c:\windows\123.bat'),nil,nil,sw_shownormal);
end;
goomoo 2002-11-21
  • 打赏
  • 举报
回复
"其实可以直接把命令些在WINEXEC里"

错误!
Dos内部命令是不能在 WinExec() 里执行的。
hacking 2002-11-21
  • 打赏
  • 举报
回复
同意楼上,其实可以直接把命令些在WINEXEC里

5,927

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧