برنامه نویس

به وبلاگ خودتان خوش آمدید.

برنامه نویس

به وبلاگ خودتان خوش آمدید.

چند نمونه کد در دلفی

یک تعداد کد روی سیستم داشتم که اینجا میذارم امیدوارم مورد استفاده دوستان واقع بشه . 

 

پاک کردن، کپی کردن و انتقال یک فایل یا فولدر   

غیر فعال کردن Task Manager  

بدست آوردن تعداد فایل های موجود در سطل آشغال به همراه سایز آنها  

بدست آوردن نام Cpu و سرعت آن 

فرمت درایو 

اجرای Task Manager 

مشخص نمودن اجراء بودن یک پروسس 

غیر فعال کردن Ctrl+Alt+Delet

 

پاک کردن، کپی کردن و انتقال یک فایل یا فولدر :

 

Directories

Create a Directory
:
* CreateDir('c:\path');

Remove a Directory : RemoveDir('c:\path') or RmDir('c:\path')
Change a Directory : ChDir('c:\path')
Current Directory : GetCurrentDir
Check if a Directory exists : if DirectoryExists('c:\path') then ...



Files

Rename a File : RenameFile('file1.txt', 'file2.xyz')
Delete a File : DeleteFile('c:\text.txt')
Move a File : MoveFile('C:\file1.txt','D:\file1.txt');
Copy a File :
* CopyFile(Pchar(File1),PChar(File2),bFailIfExists)

Change a File's Extension : ChangeFileExt('test.txt', 'xls')
Check if a File exists : if FileExists('c:\filename.tst') then ...

 

غیر فعال کردن Task Manager :

procedure DisableTaskMgr(bTF: Boolean);
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CURRENT_USER;

  reg.OpenKey('Software', True);
  reg.OpenKey('Microsoft', True);
  reg.OpenKey('Windows', True);
  reg.OpenKey('CurrentVersion', True);
  reg.OpenKey('Policies', True);
  reg.OpenKey('System', True);

  if bTF = True then
  begin
    reg.WriteString('DisableTaskMgr', '1');
  end
  else if bTF = False then
  begin
    reg.DeleteValue('DisableTaskMgr');
  end;
  reg.CloseKey;
end;
// Example Call:procedure TForm1.Button1Click(Sender: TObject);
begin
  DisableTaskMgr(True);
end;

بدست آوردن تعداد فایل های موجود در سطل آشغال به همراه سایز آنها :

type
  
PSHQueryRBInfo = ^TSHQueryRBInfo;
  TSHQueryRBInfo = packed record
    
cbSize: DWORD;
    // Size of the structure, in bytes.
    // This member must be filled in prior to calling the function.

    
i64Size: Int64;
    // Total size of all the objects in the specified Recycle Bin, in bytes.    i64NumItems: Int64;
    // Total number of items in the specified Recycle Bin.
  
end;

const
  
shell32 = 'shell32.dll';

function SHQueryRecycleBin(szRootPath: PChar; SHQueryRBInfo: PSHQueryRBInfo): HResult;
  stdcallexternal shell32 Name 'SHQueryRecycleBinA';

function GetDllVersion(FileName: string): Integer;
var
  
InfoSize, Wnd: DWORD;
  VerBuf: Pointer;
  FI: PVSFixedFileInfo;
  VerSize: DWORD;
begin
  
Result   := 0;
  InfoSize := GetFileVersionInfoSize(PChar(FileName), Wnd);
  if InfoSize <> 0 then
  begin
    
GetMem(VerBuf, InfoSize);
    try
      if 
GetFileVersionInfo(PChar(FileName), Wnd, InfoSize, VerBuf) then
        if 
VerQueryValue(VerBuf, '\', Pointer(FI), VerSize) then
          
Result := FI.dwFileVersionMS;
    finally
      
FreeMem(VerBuf);
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  
DllVersion: integer;
  SHQueryRBInfo: TSHQueryRBInfo;
  r: HResult;
begin
  
DllVersion := GetDllVersion(PChar(shell32));
  if DllVersion >= $00040048 then
  begin
    
FillChar(SHQueryRBInfo, SizeOf(TSHQueryRBInfo), #0);
    SHQueryRBInfo.cbSize := SizeOf(TSHQueryRBInfo);
    R := SHQueryRecycleBin(nil, @SHQueryRBInfo);
    if r = s_OK then
    begin
      
label1.Caption := Format('Size:%d Items:%d',
        [SHQueryRBInfo.i64Size, SHQueryRBInfo.i64NumItems]);
    end
    else
      
label1.Caption := Format('Err:%x', [r]);
  end;
end;

{

The SHQueryRecycleBin API used in this method is
only available on systems with the latest shell32.dll installed with IE4 /
Active Desktop.


}

 

 

بدست آوردن نام Cpu و سرعت آن :

 

// this code will get the cpu identifier from the windows registry
uses
  
Registry;

function CPUname: string;
var
  
Reg: TRegistry;
begin
  
CPUname := '';
  Reg := TRegistry.Create;
  
try
    
Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\Hardware\Description\System\CentralProcessor\0', False) 
then
      
CPUname := Reg.ReadString('Identifier');
  
finally
    
Reg.Free;
  end;
end;
// this code will get the cpu speed from the windows registryuses
  
Registry;

function GetCpuSpeed: string;
var
  
Reg: TRegistry;
begin
  
Reg := TRegistry.Create;
  
try
    
Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('Hardware\Description\System\CentralProcessor\0', False) 
then
    begin
      
Result := IntToStr(Reg.ReadInteger('~MHz')) + ' MHz';
      Reg.CloseKey;
    end;
  
finally
    
Reg.Free;
  end;
end;

 

فرمت درایو :

// formating a floppy drive, harddisk, or another drive
// Eine Diskette, Harddisk oder ein anderes Laufwerk formatieren


{  The SHFormatDrive API provides access to the Shell's format
  dialog box. This allows applications that want to format disks to bring
  up the same dialog box that the Shell uses for disk formatting.

}
const 
  
SHFMT_DRV_A = 0;
  SHFMT_DRV_B = 1;
  SHFMT_ID_DEFAULT = $FFFF;
  SHFMT_OPT_QUICKFORMAT = 0;
  SHFMT_OPT_FULLFORMAT = 1;
  SHFMT_OPT_SYSONLY = 2;
  SHFMT_ERROR = -1;
  SHFMT_CANCEL = -2;
  SHFMT_NOFORMAT = -3;

function SHFormatDrive(hWnd: HWND;
  Drive: Word;
  fmtID: Word;
  Options: Word): Longint
  stdcallexternal 'Shell32.dll' Name 'SHFormatDrive';


procedure TForm1.Button1Click(Sender: TObject);
var
  
FmtRes: Longint;
begin
  try
    
FmtRes := ShFormatDrive(Handle,
      SHFMT_DRV_A,
      SHFMT_ID_DEFAULT,
      SHFMT_OPT_QUICKFORMAT);
    case FmtRes 
of
      
SHFMT_ERROR: ShowMessage('Error formatting the drive');
      SHFMT_CANCEL: ShowMessage('User canceled formatting the drive');
      SHFMT_NOFORMAT: ShowMessage('No Format')
        
else
          
ShowMessage('Disk has been formatted!');
    end;
  
except
    
ShowMessage('Error Occured!');
  end;
end;
{  Normally, if a diskette is not in the drive when SHFormatDrive is called,
  the system displays a critical error dialog box that asks the user
  to Abort, Retry, or Ignore.
  You can prevent the system from displaying this dialog box by calling
  the SetErrorMode API with SEM_FAILCRITICALERRORS.
}
var
  
EMode: Word;
begin
  
EMode := SetErrorMode(SEM_FAILCRITICALERRORS);

  // ShFormatDrive Code....
  
SetErrorMode(EMode);
end;

 

اجرای Task Manager :

uses ShellApi;

procedure TForm1.Button1Click(Sender: TObject);
beginShellExecute (HWND(nil), 'open', 'taskmgr', '', '',SW_SHOWNORMAL);
end;

 

مشخص نمودن اجراء بودن یک پروسس :

uses TlHelp32;

function processExists(exeFileName: string): Boolean;var
  
ContinueLoop: BOOL;
  FSnapshotHandle: THandle;
  FProcessEntry32: TProcessEntry32;
begin
  
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
  ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
  Result := False;
  while Integer(ContinueLoop) <> 0 
do
  begin
    if 
((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
      UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
      UpperCase(ExeFileName))) 
then
    begin
      
Result := True;
    end;
    ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
  end;
  CloseHandle(FSnapshotHandle);
end;


// Example Call
:procedure TForm1.Button1Click(Sender: TObject);begin
  if 
processExists('calc.exe') 
then
    
ShowMessage('process is running')
  
else
    
ShowMessage('process NOT running');
end;

غیر فعال کردن Ctrl+Alt+Delet :

procedure DisableTaskMgr(bTF: Boolean);
var
  
reg: TRegistry;
begin
  
reg := TRegistry.Create;
  reg.RootKey := HKEY_CURRENT_USER;

  reg.OpenKey('Software', True);
  reg.OpenKey('Microsoft', True);
  reg.OpenKey('Windows', True);
  reg.OpenKey('CurrentVersion', True);
  reg.OpenKey('Policies', True);
  reg.OpenKey('System', True);

  if bTF = True then
  begin
    
reg.WriteString('DisableTaskMgr', '1');
  end
  else if 
bTF = False then
  begin
    
reg.DeleteValue('DisableTaskMgr');
  end;
  reg.CloseKey;
end;

// Example Call:procedure TForm1.Button1Click(Sender: TObject);
begin
  
DisableTaskMgr(True);
end

 

نظرات 3 + ارسال نظر
مریم تقی فارابی دوشنبه 30 آبان‌ماه سال 1390 ساعت 08:39 ب.ظ

با سلام
وبلاگت خیلی خیلی خوبه و حرف نداره عالیه
من یه کدی رو تو دلفی نوشتم در واقع یک لیسیتی از خریدها و قیمت و تاریخ خرید و جمع هزینه و ... هست و حالا من می خوام این رو روی هاردم با نوشتن یه کد ذخیره کنم اما نمی تونم این کار رو کنم اگه ممکنه کدش رو برام بنویسی و برام توضیح هم بدی.میخوام همه اینها در یک فایل ذخیره بشن.اگه ممکنه برام ایمیل بزنید.
mtfa65@gmail.com
ممنونم.

محمد رضا سیفی شنبه 1 بهمن‌ماه سال 1390 ساعت 09:05 ب.ظ http://mohamadreza.blogfa.com

چگونه برنامه برای دانلود در وبلاگم بگذارم

دلی چهارشنبه 14 فروردین‌ماه سال 1392 ساعت 08:13 ب.ظ

واعقا عالی بود خسته نباشید

برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد