Mendapatkan Path Drive dan Folder Windows dengan Delphi


Mendapatkan Path Drive dan Folder Windows dengan Delphi - Pada saat membuat aplikasi dengan Delphi kadang perlu mendeklarasikan path drive maupun path folder windows dari komputer yang akan diinstal aplikasi yang dibuat. Berikut dijelaskan dengan contoh aplikasi berikut ini,
  • Pertama tempatkan beberapa komponen pada Form seperti berikut,

  • Deklarasikan function pada private,
          private
              { Private declarations }
              function GetSystemDrive: string;
              function GetWinDir: string;
  • Buat Function seperti berikut,
          function TForm1.GetSystemDrive: string;
          begin
             SetLength(Result, MAX_PATH);
             if GetWindowsDirectory(PChar(Result), MAX_PATH) > 0 then begin
               SetLength(Result, StrLen(PChar(Result)));
               Result := ExtractFileDrive(Result);
             end else
               RaiseLastOSError;
          end;

          function TForm1.GetWinDir: string;
          var
             dir: array [0..MAX_PATH] of Char;
          begin
            GetWindowsDirectory(dir, MAX_PATH);
            Result := StrPas(dir);
          end;
  • Tambahkan pada event OnCreate dari Form1,
          procedure TForm1.FormCreate(Sender: TObject);
          begin
            Label1.Caption := 'Path Drive Windows';
            Label2.Caption := 'Path Folder Windows';
            Label3.Caption := GetSystemDrive;
            Label4.Caption := GetWinDir;
          end;


Berikut full scriptnya,

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, TeEngine, Series, ExtCtrls, TeeProcs, Chart, DbChart,
  DB, DBTables, FileCtrl, Buttons, jpeg;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    function GetSystemDrive: string;
    function GetWinDir: string;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.GetSystemDrive: string;
begin
  SetLength(Result, MAX_PATH);
  if GetWindowsDirectory(PChar(Result), MAX_PATH) > 0 then begin
    SetLength(Result, StrLen(PChar(Result)));
    Result := ExtractFileDrive(Result);
  end else
    RaiseLastOSError;
end;

function TForm1.GetWinDir: string;
var
  dir: array [0..MAX_PATH] of Char;
begin
  GetWindowsDirectory(dir, MAX_PATH);
  Result := StrPas(dir);
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
  Label1.Caption := 'Path Drive Windows';
  Label2.Caption := 'Path Folder Windows';
  Label3.Caption := GetSystemDrive;
  Label4.Caption := GetWinDir;
end;

end.


Ketentuan :
Teman-teman boleh menyebarkan tulisan ini dengan mencantumkan link berikut :
sumber : http://andsc.blogspot.com

<<<< Terima kasih >>>>

Article :

0 komentar:

Tentang Saya

Pengunjung