`TDosCommand` is an open source component of Delphi, which can easily run DOS commands in Delphi programs and obtain their output results. In this article, we'll introduce the usage of the `TDosCommand` component and demonstrate how it can be used to run DOS commands.
## Install the TDosCommand component
Download: https://codeload.github.com/TurboPack/DOSCommand/zip/refs/heads/master
Before using the `TDosCommand` component, we need to install it into the Delphi environment. Here are the installation steps:
1\ Open in Delphi 11: C:\delphisource\mytools\DOSCommand-master\DOSCommand-master\Packages\Alexandria\Delphi\DOSCommandDelphi..groupproj
2. Set the libraries path in option.
Note: In addition to adding the win32 path in step 7, the win64 must also add the same path.
3. Compile and create a dynamic library.
3. Compile and create the design library (otherwise an error will be reported)
4. Save the project file. After the installation is complete, you can find the `TDosCommand` component in the "TDoscommand" tab of the "Tool Palette".
## Use TDosCommand component
To use the `TDosCommand` component in Delphi, you need to drag and drop it on the form or data module, and then set the properties and events of `TDosCommand` in the code. The following is a sample program that demonstrates how to use the `TDosCommand` component to run DOS commands and display the output in the Memo component:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, DosCommand;
type
TForm1 = class(TForm)
Memo1: TMemo;
Panel1: TPanel;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
DosCommand1: TDosCommand;
procedure Button1Click(Sender: TObject);
procedure DosCommand1NewLine(Sender: TObject; const NewLine: string; OutputType: TOutputType);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
System.IOUtils;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
// 设置命令行参数
DosCommand1.CommandLine := 'cmd.exe /c ' + Edit1.Text;
// 启动进程
DosCommand1.Execute;
end;
procedure TForm1.DosCommand1NewLine(Sender: TObject; const NewLine: string; OutputType: TOutputType);
begin
// 在 Memo 组件中显示输出结果
Memo1.Lines.Add(NewLine);
end;
end.
In this sample program, we use the `TDosCommand` component to run DOS commands. When the button is clicked, the program will set the command line parameters to be run through the `TDosCommand.CommandLine` property, then start the process and get the output. The output is returned through the `TDosCommand.NewLine` event, where we can add the output to the Memo component.
result: