Çeşitli kod ipuçları

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

bir klasörü windows explorerda açmak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

uses
  ShellApi;

// strFolder is the folder you want to open
procedure ShowFolder(strFolder: string);
begin
  ShellExecute(Application.Handle,
    PChar('explore'),
    PChar(strFolder),
    nil,
    nil,
    SW_SHOWNORMAL);
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  strFolder = 'c:\My Documents';
begin
  ShowFolder(strFolder);
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

bir programı yada atanmış bir dosya tipini çalıştırmak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

{ Open a file or starts a programm (without parameters) }

procedure OpenFile(FileName: string);
var
  c: array[0..800] of Char;
begin
  StrPCopy(c, FileName);
  ShellExecute(Application.Handle, 'open', c, nil, nil, SW_NORMAL);
end;

{ Starts a programm with commandline parameters }

procedure OpenProgram(prog, params: string);
var
  c, p: array[0..800] of Char;
begin
  StrPCopy(c, prog);
  StrPCopy(p, params);
  ShellExecute(Application.Handle, 'open', c, p, nil, SW_NORMAL);
end;

{ Starts a program and wait until its terminated:
  WindowState is of the SW_xxx constants }

function ExecAndWait(const FileName, Params: string;
  WindowState: Word): Boolean;
var
  SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
  CmdLine: string;
begin
  { Enclose filename in quotes to take care of
    long filenames with spaces. }
  CmdLine := '"' + FileName + '"' + Params;
  FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;
  Result := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
    CREATE_NEW_CONSOLE or
    NORMAL_PRIORITY_CLASS, nil,
    PChar(ExtractFilePath(FileName)),
    SUInfo, ProcInfo);
  { Wait for it to finish. }
  if Result then
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;

{ Execute a complete shell command line and waits until terminated. }

function ExecCmdLineAndWait(const CmdLine: string;
  WindowState: Word): Boolean;
var
  SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
begin
  { Enclose filename in quotes to take care of
    long filenames with spaces. }
  FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;
  Result := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
    CREATE_NEW_CONSOLE or
    NORMAL_PRIORITY_CLASS, nil,
    nil {PChar(ExtractFilePath(Filename))},
    SUInfo, ProcInfo);
  { Wait for it to finish. }
  if Result then
    WaitForSingleObject(ProcInfo.hProcess, INFINITE);
end;

{ Execute a complete shell command line without waiting. }

function OpenCmdLine(const CmdLine: string;
  WindowState: Word): Boolean;
var
  SUInfo: TStartupInfo;
  ProcInfo: TProcessInformation;
begin
  { Enclose filename in quotes to take care of
    long filenames with spaces. }
  FillChar(SUInfo, SizeOf(SUInfo), #0);
  with SUInfo do
  begin
    cb := SizeOf(SUInfo);
    dwFlags := STARTF_USESHOWWINDOW;
    wShowWindow := WindowState;
  end;
  Result := CreateProcess(nil, PChar(CmdLine), nil, nil, False,
    CREATE_NEW_CONSOLE or
    NORMAL_PRIORITY_CLASS, nil,
    nil {PChar(ExtractFilePath(Filename))},
    SUInfo, ProcInfo);
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

tfilestream kullanımı

Mesaj gönderen ikutluay »

Kod: Tümünü seç

type

  TPerson = record
    Name: string[50];
    vorname: string[50];
  end;

  TComputer = record
    Name: string[30];
    cpu: string[30];
  end;

var
  Form1: TForm1;

  Person: TPerson;
  Computer: TComputer;

  Stream: TFileStream;

implementation

{$R *.DFM}

//Speichern resp. Erstellen von Datei
//Save or create the file
procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    Stream := TFileStream.Create('c:\test.dat', fmOpenReadWrite);
  except
    Stream := TFileStream.Create('c:\test.dat', fmCreate);
  end;

  //2 Einträge pro Record
  //save 2 records for TPerson and TComputer
  Person.Name    := 'Grossenbacher';
  Person.vorname := 'Simon';
  Stream.WriteBuffer(Person, SizeOf(TPerson));

  Person.Name    := 'Stutz';
  Person.vorname := 'Thomas';
  Stream.WriteBuffer(Person, SizeOf(TPerson));

  Computer.Name := 'Delphi';
  Computer.cpu  := 'Intel';
  Stream.WriteBuffer(Computer, SizeOf(TComputer));

  Computer.Name := 'Win';
  Computer.cpu  := 'AMD';
  Stream.WriteBuffer(Computer, SizeOf(TComputer));

  Stream.Free;
end;

//lädt alle daten von TPerson in listbox1 und
//daten von TComputer in Listbox2

//load records from TPerson to listbox1 and
//load records from TComputer to listbox2
procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
begin
  try
    // nur lesen öffnen
    //open read only
    Stream := TFileStream.Create('c:\test.dat', fmOpenRead);
  except
    ShowMessage('Datei konnte nicht geladen werden.');
    Exit;
  end;

  //variable i auf anzahl Einträge setzen

  //set variable i to the record count

  //Einlesen von TPerson
  //Read records TPerson
  for i := 2 downto 1 do
  begin
    Stream.ReadBuffer(Person, SizeOf(TPerson));
    Listbox1.Items.Add(Person.vorname + ' ' + Person.Name);
  end;

  //Einlesen von TComputer
  //Read Records TComputer
  for i := 2 downto 1 do
  begin
    Stream.ReadBuffer(Computer, SizeOf(TComputer));
    Listbox2.Items.Add(Computer.Name + ' ' + Computer.cpu);
  end;

  Stream.Free;
end;


Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

UNC yollarını almak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

ExpandUNCFileName returns the full path of the FileName
with the network drive portion in UNC format.
The pathname in the UNC-Format has the format:
\\Servername\sharename

ExpandUNCFileName gibt einen String mit dem vollständigen
Pfadnamen der in FileName übergebenen Datei zurück.
Ein vollständig qualifizierter Pfadname besteht aus der
Laufwerkskomponente des Dateinamens im UNC-Format:
\\Servername\sharename

// Example, Beispiel:

Label1.Caption := ExpandUNCFileName('K:\sharename.tmp'));

{where "K" is a Network Drive.}


{*****************************************************}
{2. Way }

function GetUNCName(const LocalPath: string): string;
var
  BufferSize: DWord;
  DummyBuffer: Byte;
  Buffer: Pointer;
  Error: DWord;
begin
  BufferSize := 1;
  WNetGetUniversalName(PChar(LocalPath), UNIVERSAL_NAME_INFO_LEVEL, @DummyBuffer, BufferSize);
  Buffer := AllocMem(BufferSize);
  try
    Error := WNetGetUniversalName(PChar(LocalPath), UNIVERSAL_NAME_INFO_LEVEL, Buffer, BufferSize);
    if Error <> NO_ERROR then
      begin
        SetLastError(Error);
        RaiseLastWin32Error;
      end;
    Result := PUniversalNameInfo(Buffer)^.lpUniversalName
  finally
    FreeMem(Buffer);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Label1.Caption := GetUNCName('y:\xyz\')
end;


Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

çalışan dll yi yeni sürümüyle değiştirme replace etmek

Mesaj gönderen ikutluay »

Kod: Tümünü seç

function SystemErrorMessage: string;
var 
  P: PChar;
begin
  if FormatMessage(Format_Message_Allocate_Buffer + Format_Message_From_System,
                   nil,
                   GetLastError,
                   0,
                   @P,
                   0,
                   nil) <> 0 then
  begin
    Result := P;
    LocalFree(Integer(P))
  end 
  else 
    Result := '';
end;


// Path to Original File

procedure TForm1.Button2Click(Sender: TObject);
begin
  if Opendialog1.Execute then
    edit1.Text := OpenDialog1.FileName;
end;

// Path to New File

procedure TForm1.Button3Click(Sender: TObject);
begin
  if Opendialog2.Execute then
    edit2.Text := OpenDialog2.FileName;
end;

// Replace the File.

procedure TForm1.Button1Click(Sender: TObject);
begin
  if (Movefileex(PChar(Edit1.Text), PChar(Edit2.Text), MOVEFILE_DELAY_UNTIL_REBOOT) = False) then
    ShowMessage(SystemErrorMessage)
  else
  begin
    ShowMessage('Please Restart Windows to have these changes take effect');
    halt;
  end;
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

dosya yada klasör silmek-kopyalamk-taşımak-isim değiştirmek

Mesaj gönderen ikutluay »

Kod: Tümünü seç

Directories

Create a Directory :
* CreateDir('c:\path');
* Tip 102 (MkDir Example)
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 ...
Rename a Directory: Tip 1024 (SHFileOperation)
Copy/Move/Delete whole directories: Tip 152 (ShFileOperation)

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)
* Tip 101 (Example)
* Tip 28 (SHFileOperation)
Change a File's Extension : ChangeFileExt('test.txt', 'xls')
Check if a File exists : if FileExists('c:\filename.tst') then ...
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

dosyanın sahibini bulmak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

// When you create a file or directory, you become the owner of it.
// With GetFileOwner you get the owner of a file.

function GetFileOwner(FileName: string;
  var Domain, Username: string): Boolean;
var
  SecDescr: PSecurityDescriptor;
  SizeNeeded, SizeNeeded2: DWORD;
  OwnerSID: PSID;
  OwnerDefault: BOOL;
  OwnerName, DomainName: PChar;
  OwnerType: SID_NAME_USE;
begin
  GetFileOwner := False;
  GetMem(SecDescr, 1024);
  GetMem(OwnerSID, SizeOf(PSID));
  GetMem(OwnerName, 1024);
  GetMem(DomainName, 1024);
  try
    if not GetFileSecurity(PChar(FileName),
      OWNER_SECURITY_INFORMATION,
      SecDescr, 1024, SizeNeeded) then
      Exit;
    if not GetSecurityDescriptorOwner(SecDescr,
      OwnerSID, OwnerDefault) then
      Exit;
    SizeNeeded  := 1024;
    SizeNeeded2 := 1024;
    if not LookupAccountSID(nil, OwnerSID, OwnerName,
      SizeNeeded, DomainName, SizeNeeded2, OwnerType) then
      Exit;
    Domain   := DomainName;
    Username := OwnerName;
  finally
    FreeMem(SecDescr);
    FreeMem(OwnerName);
    FreeMem(DomainName);
  end;
  GetFileOwner := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Domain, Username: string;
begin
  GetFileOwner('YourFile.xyz', domain, username);
  ShowMessage(username + '@' + domain);
end;

// Note: Only works unter NT.


 
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

klasör ağacını treeview a atmak

Mesaj gönderen ikutluay »

[/code]procedure TForm1.GetDirectories(Tree: TTreeView; Directory: string; Item: TTreeNode; IncludeFiles: Boolean);
var
SearchRec: TSearchRec;
ItemTemp: TTreeNode;
begin
Tree.Items.BeginUpdate;
if Directory[Length(Directory)] <> '\' then Directory := Directory + '\';
if FindFirst(Directory + '*.*', faDirectory, SearchRec) = 0 then
begin
repeat
if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] <> '.') then
begin
if (SearchRec.Attr and faDirectory > 0) then
Item := Tree.Items.AddChild(Item, SearchRec.Name);
ItemTemp := Item.Parent;
GetDirectories(Tree, Directory + SearchRec.Name, Item, IncludeFiles);
Item := ItemTemp;
end
else if IncludeFiles then
if SearchRec.Name[1] <> '.' then
Tree.Items.AddChild(Item, SearchRec.Name);
until FindNext(SearchRec) <> 0;
FindClose(SearchRec);
end;
Tree.Items.EndUpdate;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Node: TTreeNode;
Path: string;
Dir: string;
begin
Dir := 'c:\temp';
Screen.Cursor := crHourGlass;
TreeView1.Items.BeginUpdate;
try
TreeView1.Items.Clear;
GetDirectories(TreeView1, Dir, nil, True);
finally
Screen.Cursor := crDefault;
TreeView1.Items.EndUpdate;
end;
end;

Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

program windows açılınca çalışımaya ayarlamak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

uses
  Registry;

procedure SetAutoStart(AppName, AppTitle: string; bRegister: Boolean);
const
  RegKey = '\Software\Microsoft\Windows\CurrentVersion\Run';
  // or: RegKey = '\Software\Microsoft\Windows\CurrentVersion\RunOnce';
var
  Registry: TRegistry;
begin
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_LOCAL_MACHINE;
    if Registry.OpenKey(RegKey, False) then
    begin
      if bRegister = False then
        Registry.DeleteValue(AppTitle)
      else
        Registry.WriteString(AppTitle, AppName);
    end;
  finally
    Registry.Free;
  end;
end;

// Example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  // 1.Parameter: Path to your Exe-File
  // 2. Parameter: the Title of your Application
  // 3. Set (true) or Unset (false) Autorun
  SetAutoStart(ParamStr(0), 'Title of your Application', True);
end;


Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

Dosya ntelikleri ile oynamak değiştirmek

Mesaj gönderen ikutluay »

Kod: Tümünü seç

{
 To set a file's attributes, pass the name of the file and
 the attributes you want to the FileSetAttr function.
}

{
 FileSetAttr setzt die Attribute der mit FileName
 angegebenen Datei auf den Wert Attr.
}

{
  To hide a file:
  Eine Datei verstecken:
}

procedure TForm1.Button1Click(Sender: TObject);
begin
  FileSetAttr('C:\YourFile.ext', faHidden);
end;

{
  Other Files Attributes:
  Andere Dateiattribute:
}

{
  faReadOnly  $00000001 Schreibgeschützte Datei
  faHidden    $00000002 Verborgene Datei
  faSysFile   $00000004 Systemdatei
  faVolumeID  $00000008 Laufwerks-ID
  faDirectory $00000010 Verzeichnis
  faArchive   $00000020 Archivdatei
  faAnyFile   $0000003F Beliebige Datei
}


{
  You can also set some attributes at once:
  Es können auch mehrere Attribute aufs Mal gesetzt werden:
}

FileSetAttr('C:\Autoexec.bat', faReadOnly + faHidden);


{
  To remove write protection on a file:
  Den Schreibschutz einer Datei aufheben:
}

if (FileGetAttr(FileName) and faReadOnly) > 0
 then FileSetAttr(FileName, FileGetAttr(FileName) xor faReadOnly);

{
  Re-Set write protection:
  Schreibschutz wieder setzen:
}

FileSetAttr(FileName, FileGetAttr(FileName) or faReadOnly);


 
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

dosya için checksum hesaplamak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

function GetCheckSum(FileName: string): DWORD;
var
  F: file of DWORD;
  P: Pointer;
  Fsize: DWORD;
  Buffer: array [0..500] of DWORD;
begin
  FileMode := 0;
  AssignFile(F, FileName);
  Reset(F);
  Seek(F, FileSize(F) div 2);
  Fsize := FileSize(F) - 1 - FilePos(F);
  if Fsize > 500 then Fsize := 500;
  BlockRead(F, Buffer, Fsize);
  Close(F);
  P := @Buffer;
  asm
     xor eax, eax
     xor ecx, ecx
     mov edi , p
     @again:
       add eax, [edi + 4*ecx]
       inc ecx
       cmp ecx, fsize
     jl @again
     mov @result, eax
   end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(GetCheckSum('c:\Autoexec.bat')));
end;


Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

Başlat menusune yada masaüstüne kısayol

Mesaj gönderen ikutluay »

Kod: Tümünü seç

uses
  Registry,
  ActiveX,
  ComObj,
  ShlObj;

type
  ShortcutType = (_DESKTOP, _QUICKLAUNCH, _SENDTO, _STARTMENU, _OTHERFOLDER);

function CreateShortcut(SourceFileName: string; // the file the shortcut points to
                        Location: ShortcutType; // shortcut location
                        SubFolder,  // subfolder of location
                        WorkingDir, // working directory property of the shortcut
                        Parameters,
                        Description: string): //  description property of the shortcut
                        string;
const
  SHELL_FOLDERS_ROOT = 'Software\MicroSoft\Windows\CurrentVersion\Explorer';
  QUICK_LAUNCH_ROOT = 'Software\MicroSoft\Windows\CurrentVersion\GrpConv';
var
  MyObject: IUnknown;
  MySLink: IShellLink;
  MyPFile: IPersistFile;
  Directory, LinkName: string;
  WFileName: WideString;
  Reg: TRegIniFile;
begin

  MyObject := CreateComObject(CLSID_ShellLink);
  MySLink := MyObject as IShellLink;
  MyPFile := MyObject as IPersistFile;

  MySLink.SetPath(PChar(SourceFileName));
  MySLink.SetArguments(PChar(Parameters));
  MySLink.SetDescription(PChar(Description));

  LinkName := ChangeFileExt(SourceFileName, '.lnk');
  LinkName := ExtractFileName(LinkName);

  // Quicklauch
  if Location = _QUICKLAUNCH then
  begin
    Reg := TRegIniFile.Create(QUICK_LAUNCH_ROOT);
    try
      Directory := Reg.ReadString('MapGroups', 'Quick Launch', '');
    finally
      Reg.Free;
    end;
  end
  else
  // Other locations
  begin
    Reg := TRegIniFile.Create(SHELL_FOLDERS_ROOT);
    try
    case Location of
      _OTHERFOLDER : Directory := SubFolder;
      _DESKTOP     : Directory := Reg.ReadString('Shell Folders', 'Desktop', '');
      _STARTMENU   : Directory := Reg.ReadString('Shell Folders', 'Start Menu', '');
      _SENDTO      : Directory := Reg.ReadString('Shell Folders', 'SendTo', '');
    end;
    finally
      Reg.Free;
    end;
  end;

  if Directory <> '' then
  begin
    if (SubFolder <> '') and (Location <> _OTHERFOLDER) then
      WFileName := Directory + '\' + SubFolder + '\' + LinkName
    else
      WFileName := Directory + '\' + LinkName;


    if WorkingDir = '' then
      MySLink.SetWorkingDirectory(PChar(ExtractFilePath(SourceFileName)))
    else
      MySLink.SetWorkingDirectory(PChar(WorkingDir));

    MyPFile.Save(PWChar(WFileName), False);
    Result := WFileName;
  end;
end;

function GetProgramDir: string;
var
  reg: TRegistry;
begin
  reg := TRegistry.Create;
  try
    reg.RootKey := HKEY_CURRENT_USER;
    reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders', False);
    Result := reg.ReadString('Programs');
    reg.CloseKey;
  finally
    reg.Free;
  end;
end;

// Some examples:

procedure TForm1.Button1Click(Sender: TObject);
const
 PROGR = 'c:\YourProgram.exe';
var
  resPath: string;
begin
  //Create a Shortcut in the Quckick launch toolbar
  CreateShortcut(PROGR, _QUICKLAUNCH, '','','','Description');

  //Create a Shortcut on the Desktop
  CreateShortcut(PROGR, _DESKTOP, '','','','Description');

  //Create a Shortcut in the Startmenu /"Programs"-Folder
  resPath := CreateShortcut(PROGR, _OTHERFOLDER, GetProgramDir,'','','Description');
  if resPath <> '' then
  begin
    ShowMessage('Shortcut Successfully created in: ' + resPath);
  end;
end;


Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

Dosya metnini stringe almak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

function GetTextFromFile(AFile: string; var Returnstring: string): Boolean;
var
  FileStream: TFileStream;
begin
  Result := False;
  if not FileExists(AFile) then Exit;
  FileStream := TFileStream.Create(AFile, fmOpenRead);
  try
    if FileStream.Size <> 0 then
    begin
      SetLength(Returnstring, FileStream.Size);
      FileStream.Read(Returnstring[1], FileStream.Size);
      Result := True;
    end;
  finally
    FileStream.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
begin
  if GetTextFromFile('c:\autoexec.bat', s) then
  begin
    ShowMessage(s);
    // Label1.caption := s; or assign the text to a Label
    // Memo1.text := s;     or a memo
  end;
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

İki dosya aynımı text eden kodlar

Mesaj gönderen ikutluay »

Kod: Tümünü seç

{1.}

function Are2FilesEqual(const File1, File2: TFileName): Boolean;
var
  ms1, ms2: TMemoryStream;
begin
  Result := False;
  ms1 := TMemoryStream.Create;
  try
    ms1.LoadFromFile(File1);
    ms2 := TMemoryStream.Create;
    try
      ms2.LoadFromFile(File2);
      if ms1.Size = ms2.Size then
        Result := CompareMem(ms1.Memory, ms2.memory, ms1.Size);
    finally
      ms2.Free;
    end;
  finally
    ms1.Free;
  end
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if Opendialog1.Execute then
    if Opendialog2.Execute then
      if Are2FilesEqual(Opendialog1.FileName, Opendialog2.FileName) then
        ShowMessage('Files are equal.');
end;

{********************************************}

{2.}

function FilesAreEqual(const File1, File2: TFileName): Boolean;
const  
  BlockSize = 65536;
var  
  fs1, fs2: TFileStream;  
  L1, L2: Integer;  
  B1, B2: array[1..BlockSize] of Byte;
begin  
  Result := False;  
  fs1 := TFileStream.Create(File1, fmOpenRead or fmShareDenyWrite);
  try    
    fs2 := TFileStream.Create(File2, fmOpenRead or fmShareDenyWrite);
    try      
      if fs1.Size = fs2.Size then 
      begin        
        while fs1.Position < fs1.Size do 
        begin          
          L1 := fs1.Read(B1[1], BlockSize);
          L2 := fs2.Read(B2[1], BlockSize);
          if L1 <> L2 then 
          begin            
            Exit;
          end;          
          if not CompareMem(@B1[1], @B2[1], L1) then Exit;        
        end;        
        Result := True;      
      end;    
    finally      
      fs2.Free;    
    end;  
  finally    
    fs1.Free;  
  end;
end;
Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
ikutluay
Üye
Mesajlar: 2341
Kayıt: 03 Tem 2007 10:13

CRC32 değerinden dosya tanımak

Mesaj gönderen ikutluay »

Kod: Tümünü seç

// The constants here are for the CRC-32 generator
// polynomial, as defined in the Microsoft
// Systems Journal, March 1995, pp. 107-108
const
  Table: array[0..255] of DWORD =
    ($00000000, $77073096, $EE0E612C, $990951BA,
    $076DC419, $706AF48F, $E963A535, $9E6495A3,
    $0EDB8832, $79DCB8A4, $E0D5E91E, $97D2D988,
    $09B64C2B, $7EB17CBD, $E7B82D07, $90BF1D91,
    $1DB71064, $6AB020F2, $F3B97148, $84BE41DE,
    $1ADAD47D, $6DDDE4EB, $F4D4B551, $83D385C7,
    $136C9856, $646BA8C0, $FD62F97A, $8A65C9EC,
    $14015C4F, $63066CD9, $FA0F3D63, $8D080DF5,
    $3B6E20C8, $4C69105E, $D56041E4, $A2677172,
    $3C03E4D1, $4B04D447, $D20D85FD, $A50AB56B,
    $35B5A8FA, $42B2986C, $DBBBC9D6, $ACBCF940,
    $32D86CE3, $45DF5C75, $DCD60DCF, $ABD13D59,
    $26D930AC, $51DE003A, $C8D75180, $BFD06116,
    $21B4F4B5, $56B3C423, $CFBA9599, $B8BDA50F,
    $2802B89E, $5F058808, $C60CD9B2, $B10BE924,
    $2F6F7C87, $58684C11, $C1611DAB, $B6662D3D,

    $76DC4190, $01DB7106, $98D220BC, $EFD5102A,
    $71B18589, $06B6B51F, $9FBFE4A5, $E8B8D433,
    $7807C9A2, $0F00F934, $9609A88E, $E10E9818,
    $7F6A0DBB, $086D3D2D, $91646C97, $E6635C01,
    $6B6B51F4, $1C6C6162, $856530D8, $F262004E,
    $6C0695ED, $1B01A57B, $8208F4C1, $F50FC457,
    $65B0D9C6, $12B7E950, $8BBEB8EA, $FCB9887C,
    $62DD1DDF, $15DA2D49, $8CD37CF3, $FBD44C65,
    $4DB26158, $3AB551CE, $A3BC0074, $D4BB30E2,
    $4ADFA541, $3DD895D7, $A4D1C46D, $D3D6F4FB,
    $4369E96A, $346ED9FC, $AD678846, $DA60B8D0,
    $44042D73, $33031DE5, $AA0A4C5F, $DD0D7CC9,
    $5005713C, $270241AA, $BE0B1010, $C90C2086,
    $5768B525, $206F85B3, $B966D409, $CE61E49F,
    $5EDEF90E, $29D9C998, $B0D09822, $C7D7A8B4,
    $59B33D17, $2EB40D81, $B7BD5C3B, $C0BA6CAD,

    $EDB88320, $9ABFB3B6, $03B6E20C, $74B1D29A,
    $EAD54739, $9DD277AF, $04DB2615, $73DC1683,
    $E3630B12, $94643B84, $0D6D6A3E, $7A6A5AA8,
    $E40ECF0B, $9309FF9D, $0A00AE27, $7D079EB1,
    $F00F9344, $8708A3D2, $1E01F268, $6906C2FE,
    $F762575D, $806567CB, $196C3671, $6E6B06E7,
    $FED41B76, $89D32BE0, $10DA7A5A, $67DD4ACC,
    $F9B9DF6F, $8EBEEFF9, $17B7BE43, $60B08ED5,
    $D6D6A3E8, $A1D1937E, $38D8C2C4, $4FDFF252,
    $D1BB67F1, $A6BC5767, $3FB506DD, $48B2364B,
    $D80D2BDA, $AF0A1B4C, $36034AF6, $41047A60,
    $DF60EFC3, $A867DF55, $316E8EEF, $4669BE79,
    $CB61B38C, $BC66831A, $256FD2A0, $5268E236,
    $CC0C7795, $BB0B4703, $220216B9, $5505262F,
    $C5BA3BBE, $B2BD0B28, $2BB45A92, $5CB36A04,
    $C2D7FFA7, $B5D0CF31, $2CD99E8B, $5BDEAE1D,

    $9B64C2B0, $EC63F226, $756AA39C, $026D930A,
    $9C0906A9, $EB0E363F, $72076785, $05005713,
    $95BF4A82, $E2B87A14, $7BB12BAE, $0CB61B38,
    $92D28E9B, $E5D5BE0D, $7CDCEFB7, $0BDBDF21,
    $86D3D2D4, $F1D4E242, $68DDB3F8, $1FDA836E,
    $81BE16CD, $F6B9265B, $6FB077E1, $18B74777,
    $88085AE6, $FF0F6A70, $66063BCA, $11010B5C,
    $8F659EFF, $F862AE69, $616BFFD3, $166CCF45,
    $A00AE278, $D70DD2EE, $4E048354, $3903B3C2,
    $A7672661, $D06016F7, $4969474D, $3E6E77DB,
    $AED16A4A, $D9D65ADC, $40DF0B66, $37D83BF0,
    $A9BCAE53, $DEBB9EC5, $47B2CF7F, $30B5FFE9,
    $BDBDF21C, $CABAC28A, $53B39330, $24B4A3A6,
    $BAD03605, $CDD70693, $54DE5729, $23D967BF,
    $B3667A2E, $C4614AB8, $5D681B02, $2A6F2B94,
    $B40BBE37, $C30C8EA1, $5A05DF1B, $2D02EF8D);

type
//----------------------------------crc32----------------------------------
  {$IFDEF VER130}           // This is a bit awkward
    // 8-byte integer
    TInteger8 = Int64;     // Delphi 5
  {$ELSE}
  {$IFDEF VER120}
    TInteger8 = Int64;     // Delphi 4
  {$ELSE}
    TInteger8 = COMP;      // Delphi  2 or 3
  {$ENDIF}
  {$ENDIF}
//----------------------------------crc32----------------------------------

  
  // Use CalcCRC32 as a procedure so CRCValue can be passed in but
  // also returned. This allows multiple calls to CalcCRC32 for
  // the "same" CRC-32 calculation.
procedure CalcCRC32(p: Pointer; ByteCount: DWORD; var CRCValue: DWORD);
  // The following is a little cryptic (but executes very quickly).
  // The algorithm is as follows:
  // 1. exclusive-or the input byte with the low-order byte of
  // the CRC register to get an INDEX
  // 2. shift the CRC register eight bits to the right
  // 3. exclusive-or the CRC register with the contents of Table[INDEX]
  // 4. repeat steps 1 through 3 for all bytes
var
  i: DWORD;
  q: ^BYTE;
begin
  q := p;
  for i := 0 to ByteCount - 1 do
  begin
    CRCvalue := (CRCvalue shr 8) xor
      Table[q^ xor (CRCvalue and $000000FF)];
    Inc(q)
  end
end {CalcCRC32};

function CalcStringCRC32(s: string; out CRC32: DWORD): Boolean;
var
  CRC32Table: DWORD;
begin
  // Verify the table used to compute the CRCs has not been modified.
  // Thanks to Gary Williams for this suggestion, Jan. 2003.
  CRC32Table := $FFFFFFFF;
  CalcCRC32(Addr(Table[0]), SizeOf(Table), CRC32Table);
  CRC32Table := not CRC32Table;

  if CRC32Table <> $6FCF9E13 then ShowMessage('CRC32 Table CRC32 is ' +
      IntToHex(Crc32Table, 8) +
      ', expecting $6FCF9E13')
  else
  begin
    CRC32 := $FFFFFFFF; // To match PKZIP
    if Length(s) > 0  // Avoid access violation in D4
      then CalcCRC32(Addr(s[1]), Length(s), CRC32);
    CRC32 := not CRC32; // To match PKZIP
  end;
end;

procedure CalcFileCRC32(FromName: string; var CRCvalue: DWORD;
  var TotalBytes: TInteger8;
  var error: Word);
var
  Stream: TMemoryStream;
begin
  error := 0;
  CRCValue := $FFFFFFFF;
  Stream := TMemoryStream.Create;
  try
    try
      Stream.LoadFromFile(FromName);
      if Stream.Size > 0 then CalcCRC32(Stream.Memory, Stream.Size, CRCvalue)
      except
        on E: EReadError do
          error := 1
    end;
    CRCvalue := not CRCvalue
  finally
    Stream.Free
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s: string;
  CRC32: DWORD;
begin
  s := 'Test String';
  if CalcStringCRC32(s, CRC32) then
    ShowMessage(IntToStr(crc32));
end;

Kişi odur ki, koyar dünyada bir eser. Eseri olmayanın yerinde yeller eser./Muhammed Hadimi
http://www.ibrahimkutluay.net
http://www.ibrahimkutluay.net/blog
Cevapla