Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 4660|回复: 2

调用外部程序,用process的管道获得外部程序的输出信息......

[复制链接]

该用户从未签到

发表于 2009-11-10 17:17:57 | 显示全部楼层 |阅读模式
var
   AProcess: TProcess;
   AStringList: TStringList;
begin
   AProcess := TProcess.Create(nil);
   AStringList := TStringList.Create;
   AProcess.CommandLine :='D:\wget\wget.exe  -t 0 -c  文件的下载地址';
   AProcess.Options := AProcess.Options + [poWaitOnExit,poUsePipes];
   AProcess.Execute;
   AStringList.LoadFromStream(AProcess.Output);
   AStringList.SaveToFile('output.txt');
   AProcess.Free;
end;
为什么output.txt没有任何信息,...我想通过wget这个程序的输出信息,判断文件是否成功下载完毕.......
回复

使用道具 举报

该用户从未签到

发表于 2010-3-31 16:31:35 | 显示全部楼层
That's nice, but how do I read the Output of a program that I have run?
Well, let's expand our example a little and do just that:

// This is a demo program that shows how to launch
// an external program and read from it's output.
program launchprogram;

// Here we include files that have useful functions
// and procedures we will need.
uses
   Classes, SysUtils, Process;

// This is defining the var "AProcess" as a variable
// of the type "TProcess"
// Also now we are adding a TStringList to store the
// data read from the programs output.
var
   AProcess: TProcess;
   AStringList: TStringList;

// This is where our program starts to run
begin
   // Now we will create the TProcess object, and
   // assign it to the var AProcess.
   AProcess := TProcess.Create(nil);

   // Create the TStringList object.
   AStringList := TStringList.Create;

   // Tell the new AProcess what the command to execute is.
   // Let's use the FreePascal compiler
   AProcess.CommandLine := 'ppc386 -h';

   // We will define an option for when the program
   // is run. This option will make sure that our program
   // does not continue until the program we will launch
   // has stopped running. Also now we will tell it that
   // we want to read the output of the file.
   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];

   // Now that AProcess knows what the commandline is
   // we will run it.
   AProcess.Execute;

   // This is not reached until ppc386 stops running.

   // Now read the output of the program we just ran
   // into the TStringList.
   AStringList.LoadFromStream(AProcess.Output);

   // Save the output to a file.
   AStringList.SaveToFile('output.txt');

   // Now that the file is saved we can free the
   // TStringList and the TProcess.
   AStringList.Free;
   AProcess.Free;   
end.
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2010-4-1 12:02:54 | 显示全部楼层
我是这样写的
  1. procedure WriteLog(strFile, strTxt: string; i: integer);       //写日志,做调试用
  2. var
  3.   wText: TextFile;
  4.   f, strType: string;
  5. begin
  6.   case i of
  7.     0:   //接收日志时
  8.     begin
  9.       strType := '接收日志';
  10.     end;
  11.     1:   //
  12.     begin
  13.       strType := '更新数据';
  14.     end;
  15.   end;
  16.    
  17.   f := ExtractFilePath(Paramstr(0)) + strFile;
  18.   AssignFile(wText, f);
  19.   if not fileExists(f) then
  20.     ReWrite(wText);
  21.   Append(wText);
  22.   Writeln(wText, FormatDatetime('yyyy-MM-dd HH:mm:ss', now)
  23.       + ' --- ' + strType + ' --- '  + strTxt);
  24.   CloseFile(wText);
  25. end;   
复制代码
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-7-2 01:09 , Processed in 0.033894 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表