20-06-19, 05:02 PM
check if a path exists
كود :
uses FileCtrl;
procedure TForm1.Button1Click(Sender: TObject);
begin
if DirectoryExists('c:\windows') then
ShowMessage('Path exists!');
end;copy files
كود :
var
fileSource, fileDest: string;
begin
fileSource := 'C:\SourceFile.txt';
fileDest := 'G:\DestFile.txt';
CopyFile(PChar(fileSource), PChar(fileDest), False);
end;create a directory
كود :
uses
Dialogs;
begin
{$I-}
MkDir('c:\windows');
{$I+}
if IOResult <> 0 then
MessageDlg('Cannot Create Directory/Verzeichnis kann nicht angelegt werden!',
mtWarning, [mbOK], 0)
else
MessageDlg('Directory Created/Neues Verzeichnis angelegt.', mtInformation, [mbOK], 0);
end;Extract Icons from a File
كود :
uses
shellApi;
{...}
procedure TForm1.Button1Click(Sender: TObject);
const
ExtrFileName = 'C:\WINNT\system32\moricons.dll';
var
icon: TIcon;
NumberOfIcons, i: Integer;
begin
icon := TIcon.Create;
try
// Get the number of Icons
NumberOfIcons := ExtractIcon(Handle, PChar(ExtrFileName), UINT(-1));
ShowMessage(Format('%d Icons', [NumberOfIcons]));
// Extract the first 5 icons
for i := 1 to 5 do
begin
// Extract an icon
icon.Handle := ExtractIcon(Handle, PChar(ExtrFileName), i);
// Draw the icon on your form
DrawIcon(Form1.Canvas.Handle, 10, i * 40, icon.Handle);
end;
finally
icon.Free;
end;
end;
// Note: If you are not using Delphi 4 you can remove the UINT.get your program's directory
كود :
procedure TForm1.Button1Click(Sender: TObject);
var
sExePath: string;
begin
sExePath := ExtractFilePath(Application.ExeName)
ShowMessage(sExePath);
end;
{
To get your program's Exe-Name:
Und den Exe-Name:
}
procedure TForm1.Button2Click(Sender: TObject);
var
sExeName: string;
begin
sExeName := ExtractFileName(Application.ExeName);
ShowMessage(sExeName);
end;
{
Instead of Application.ExeName you can also use Paramstr(0)
Anstatt Application.ExeName kann man auch Paramstr(0) einsetzen
}
{
If you are working on a DLL and are interested in the filename of the
DLL rather than the filename of the application, then you can use this function:
}
function GetModuleName: string;
var
szFileName: array[0..MAX_PATH] of Char;
begin
FillChar(szFileName, SizeOf(szFileName), #0);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
Result := szFileName;
end;get/set the current directory
كود :
// GetCurrentDir returns the fully qualified name of the current directory.
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.Caption := GetCurrentDir;
end;
// The SetCurrentDir function sets the current directory:
procedure TForm1.Button1Click(Sender: TObject);
begin
SetCurrentDir('c:\windows');
end;write to/read text files
كود :
// Create a new text file and write some text into it
procedure NewTxt;
var
f: Textfile;
begin
AssignFile(f, 'c:\ek.txt'); {Assigns the Filename}
ReWrite(f); {Create a new file named ek.txt}
Writeln(f, 'You have written text into a .txt file');
Closefile(f); {Closes file F}
end;
// Open existing text file and append some text
procedure OpenTxt;
var
F: Textfile;
begin
AssignFile(f, 'c:\ek.txt'); {Assigns the Filename}
Append(f); {Opens the file for editing}
Writeln(f, 'You have written text into a .txt file');
Closefile(f); {Closes file F}
end;
// Open existing text file and show first line
procedure ReadTxt;
var
F: Textfile;
str: string;
begin
AssignFile(f, 'c:\ek.txt'); {Assigns the Filename}
Reset(f); {Opens the file for reading}
Readln(f, str);
ShowMessage('1. line of textfile:' + str);
Closefile(f); {Closes file F}
end;