كد هاي دلفي در ويندوز

aliila1

Member
با اين كدها كارهاي ابتدايي ويندوز مثل ساخت فايل
كپي فايل - حذف فايل وفولدر تغيير نام فايل وفولدر مسير سيستم و ويندوز و تمپ

تمامش در مثال است و قابل دانلود است



procedure TForm1.Button1Click(Sender: TObject);
begin
//StrToFile('ali reza habibi' ,'c:\test.txt');
StrToFile(Edit1.text ,'c:\test.txt') ;
ShowMessage('Creat file : c:\test.txt');
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
CreateDir('c:\data');
ShowMessage('CreateDir : c:\data');
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
RemoveDirectory('C:\data');
//or RemoveDir('C:\data') //or RmDir('C:\data')
ShowMessage(' RemoveDirectory : c:\data');
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
DeleteFile('c:\test.txt');
ShowMessage('DeleteFile : c:\test.txt');
end;

procedure TForm1.Button5Click(Sender: TObject);
var
PWindowsDir: array [0..255] of Char;
begin
GetWindowsDirectory(PWindowsDir,255);
// GetSystemDirectory(PWindowsDir, MAX_PATH);
Label1.Caption:=StrPas(PWindowsDir);

end;

procedure ShellFileOperation(fromFile: string; toFile: string; Flags: Integer);
var
shellinfo: TSHFileOpStructA;
begin
with shellinfo do
begin
wnd := Application.Handle;
wFunc := Flags;
pFrom := PChar(fromFile);
pTo := PChar(toFile);
end;
SHFileOperation(shellinfo);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
//ShellFileOperation('c:\test.txt', 'c:\test2.txt', FO_COPY);
CopyFile('c:\test.txt','d:\test2.txt',true);
ShowMessage('CopyFile : c:\test.txt to d:\test2.txt');

end;

procedure TForm1.Button7Click(Sender: TObject);
begin
//ShellFileOperation('c:\test.txt', 'd:\test2.txt', FO_MOVE);
MoveFile('c:\test.txt','d:\test2.txt');
ShowMessage('MoveFile : c:\test.txt to d:\test2.txt');
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
RenameFile('c:\test.txt', 'c:\test2.dll');
ShowMessage(' RenameFile : c:\test.txt to c:\test2.dll');


end;

function CopyDir(const fromDir, toDir: string): Boolean;
var
fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do
begin
wFunc := FO_COPY;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end;
Result := (0 = ShFileOperation(fos));
end;


function MoveDir(const fromDir, toDir: string): Boolean;
var
fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do
begin
wFunc := FO_MOVE;
fFlags := FOF_FILESONLY;
pFrom := PChar(fromDir + #0);
pTo := PChar(toDir)
end;
Result := (0 = ShFileOperation(fos));
end;

function DelDir(dir: string): Boolean;
var
fos: TSHFileOpStruct;
begin
ZeroMemory(@fos, SizeOf(fos));
with fos do
begin
wFunc := FO_DELETE;
fFlags := FOF_SILENT or FOF_NOCONFIRMATION;
pFrom := PChar(dir + #0);
end;
Result := (0 = ShFileOperation(fos));
end;





procedure TForm1.Button8Click(Sender: TObject);
begin
if CopyDir('d:\download', 'e:\') = True then
ShowMessage('Directory copied.');
end;

procedure TForm1.Button10Click(Sender: TObject);
begin
DelDir('d:\download');
ShowMessage('RemoveDirectory : d:\download');
end;

procedure TForm1.Button11Click(Sender: TObject);
begin
if MoveDir('d:\download', 'e:\') = True then
ShowMessage('Directory Moved.');
end;

procedure TForm1.Button12Click(Sender: TObject);
var
PWindowsDir: array [0..255] of Char;
begin
GetSystemDirectory(PWindowsDir, MAX_PATH);
Label1.Caption:=StrPas(PWindowsDir);

end;

procedure TForm1.Button13Click(Sender: TObject);
var
PWindowsDir: array [0..255] of Char;
begin
GetTempPath(MAX_PATH,PWindowsDir);
Label1.Caption:=StrPas(PWindowsDir);

end;

procedure ShowFolder(strFolder: string);
begin
ShellExecute(Application.Handle,
PChar('explore'),
PChar(strFolder),
nil,
nil,
SW_SHOWNORMAL);
end;

procedure TForm1.Button14Click(Sender: TObject);
begin
ShowFolder('c:\data');
end;


procedure RenameDir(DirFrom, DirTo: string);
var
shellinfo: TSHFileOpStruct;
begin
with shellinfo do
begin
Wnd := 0;
wFunc := FO_RENAME;
pFrom := PChar(DirFrom);
pTo := PChar(DirTo);
fFlags := FOF_FILESONLY or FOF_ALLOWUNDO or
FOF_SILENT or FOF_NOCONFIRMATION;
end;
SHFileOperation(shellinfo);
end;

procedure TForm1.Button15Click(Sender: TObject);
begin
// RenameDir('c:\data', 'c:\data2');
RenameFile('c:\data', 'c:\data2');
ShowMessage('Rename Directory : c:\data to c:\data2');
end;

procedure TForm1.Button16Click(Sender: TObject);
begin
WinExec('rundll32.exe url.dll,FileProtocolHandler '+'notepad.exe' ,SW_NORMAL);
// WinExec('rundll32.exe url.dll,FileProtocolHandler '+[filePath] , SW_NORMAL);

WinExec('rundll32.exe shell32.dll,OpenAs_RunDLL '+'notepad.exe' ,SW_NORMAL);

end;




دانلود مثال
 

جدیدترین ارسال ها

بالا