Programlama yapalım ve Öğrenelim. - Delphi Eğitim177
  Ana Sayfa
  .NET Eğitim Notları
  Visual C# .NET Örnek Kodları
  VisualBasic.NET Örnek Kodları
  J# Örnekleri
  ASP.NET Örnek Kodları
  Delphi Eğitim
  => Delphi Eğitim1
  => Delphi Eğitim2
  => Delphi Eğitim3
  => Delphi Eğitim4
  => Delphi Eğitim5
  => Delphi Eğitim6
  => Delphi Eğitim7
  => Delphi Eğitim8
  => Delphi Eğitim9
  => Delphi Eğitim10
  => Delphi Eğitim11
  => Delphi Eğitim13
  => Delphi Eğitim14
  => Delphi Eğitim15
  => Delphi Eğitim16
  => Delphi Eğitim17
  => Delphi Eğitim18
  => Delphi Eğitim19
  => Delphi Eğitim20
  => Delphi Eğitim21
  => Delphi Eğitim22
  => Delphi Eğitim23
  => Delphi Eğitim24
  => Delphi Eğitim25
  => Delphi Eğitim26
  => Delphi Eğitim27
  => Delphi Eğitim28
  => Delphi Eğitim29
  => Delphi Eğitim30
  => Delphi Eğtim31
  => Delphi Eğitim32
  => Delphi Eğitim33
  => Delphi Eğitim34
  => Delphi Eğitim35
  => Delphi Eğitim36
  => Delphi Eğitim37
  => Delphi Eğitim38
  => Delphi Eğitim39
  => Delphi Eğitim40
  => Delphi Eğitim41
  => Delphi Eğitim42
  => Delphi Eğitim43
  => Delphi Eğitim44
  => Delphi Eğitim45
  => Delphi Eğitim46
  => Delphi Eğitim47
  => Delphi Eğitim48
  => Delphi Eğitim49
  => Delphi Eğitim50
  => Delphi Eğitim51
  => Delphi Eğitim52
  => Delphi Eğitim53
  => Delphi Eğitim54
  => Delphi Eğitim55
  => Delphi Eğitim56
  => Delphi Eğitim57
  => Delphi Eğitim58
  => Delphi Eğitim59
  => Delphi Eğitim60
  => Delphi Eğitim61
  => Delphi Eğitim62
  => Delphi Eğitim63
  => Delphi Eğitim64
  => Delphi Eğitim65
  => Delphi Eğitim66
  => Delphi Eğitim67
  => Delphi Eğitim68
  => Delphi Eğitim69
  => Delphi Eğitim70
  => Delphi Eğitim71
  => Delphi Eğitim72
  => Delphi Eğitim73
  => Delphi Eğitim74
  => Delphi Eğitim75
  => Delphi Eğitim76
  => Delphi Eğitim77
  => Delphi Eğitim78
  => Delphi Eğitim79
  => Delphi Eğitim80
  => Delphi Eğitim81
  => Delphi Eğitim82
  => Delphi Eğitim83
  => Delphi Eğitim84
  => Delphi Eğitim85
  => Delphi Eğitim86
  => Delphi Eğitim87
  => Delphi Eğitim88
  => Delphi Eğitim89
  => Delphi Eğitim90
  => Delphi Eğitim91
  => Delphi Eğitim92
  => Delphi Eğitim93
  => Delphi Eğitim94
  => Delphi Eğitim95
  => Delphi Eğitim96
  => Delphi Eğitim97
  => Delphi Eğitim98
  => Delphi Eğitim99
  => Delphi Eğitim100
  => Delphi Eğitim101
  => Delphi Eğitim102
  => Delphi Eğitim103
  => Delphi Eğitim104
  => Delphi Eğitim105
  => Delphi Eğitim106
  => Delphi Eğitim107
  => Delphi Eğitim108
  => Delphi Eğitim109
  => Delphi Eğitim110
  => Delphi Eğitim111
  => Delphi Eğitim112
  => Delphi Eğitim113
  => Delphi Eğitim114
  => Delphi Eğitim115
  => Delphi Eğitim116
  => Delphi Eğitim117
  => Delphi Eğitim118
  => Delphi Eğitim119
  => Delphi Eğitim120
  => Delphi Eğitim121
  => Delphi Eğitim122
  => Delphi Eğitim123
  => Delphi Eğitim124
  => Delphi Eğitim125
  => Delphi Eğitim126
  => Delphi Eğitim127
  => Delphi Eğitim128
  => Delphi Eğitim129
  => Delphi Eğitim130
  => Delphi Eğitim131
  => Delphi Eğitim132
  => Delphi Eğitim133
  => Delphi Eğitim134
  => Delphi Eğitim135
  => Delphi Eğitim136
  => Delphi Eğitim137
  => Delphi Eğitim138
  => Delphi Eğitim139
  => Delphi Eğitim140
  => Delphi Eğitim141
  => Delphi Eğitim142
  => Delphi Eğitim143
  => Delphi Eğitim144
  => Delphi Eğitim145
  => Delphi Eğitim146
  => Delphi eğitim147
  => Delphi Eğitim148
  => Delphi Eğitim149
  => Delphi Eğitim150
  => Delphi Eğitim151
  => Delphi Eğitim152
  => Delphi Eğitim153
  => Delphi Eğitim154
  => Delphi Eğitim155
  => Delphi Eğitim156
  => Delphi Eğitim157
  => Delphi Eğitim158
  => Delphi Eğitim159
  => Delphi Eğitim160
  => Delphi Eğitim161
  => Delphi Eğitim162
  => Delphi Eğitim164
  => Delphi Eğitim165
  => Delphi Eğitim166
  => Delphi Eğitim167
  => Delphi Eğitim168
  => Delphi Eğitim169
  => Delphi Eğitim170
  => Delphi Eğitim171
  => Delphi Eğitim172
  => Delphi Eğitim173
  => Delphi Eğitim174
  => Delphi Eğitim175
  => Delphi Eğitim176
  => Delphi Eğitim177
  => Delphi Eğitim178
  => Delphi Eğitim179
  => Delphi Eğitim180
  => Delphi Eğitim181
  => Delphi Eğitim182
  => Delphi Eğitim183
  => Delphi Eğitim184
  => Delphi Eğitim185
  => Delphi Eğitim186
  => Delphi Eğitim187
  => Delphi Eğitim188
  => Delphi Eğitim189
  => Delphi Eğitim190
  => Delphi Eğitim191
  => Delphi Eğitim192
  => Delphi Eğitim193
  => Delphi Eğitim194
  => Delphi Eğitim195
  => Delphi Eğitim196
  => Delphi Eğitim197
  => Delphi Eğitim198
  => Delphi Eğitim199
  => Delphi Eğitim200
  => Delphi Eğitim201
  => Delphi Eğitim202
  => Delphi Eğitim203
  => Delphi Eğitim204
  => Delphi Eğitim205
  => Delphi Eğitim206
  => Delphi Eğitim207
  => Delphi Eğitim208
  => Delphi Eğitim209
  => Delphi Eğitim210
  => Delphi Eğitim211
  => Delphi Eğitim212
  => Delphi Eğitim213
  => Delphi Eğitim214
  => Delphi Eğitim215
  => Delphi Eğitim216
  => Delphi Eğitim217
  => Delphi Eğitim218
  => Delphi Eğitim219
  => Delphi Eğitim220
  => Delphi Eğitim221
  => Delphi Eğitim222
  => Delphi Eğitim223
  => Delphi Eğitim224
  => Delphi Eğitim225
  => Delphi Eğitim226
  => Delphi Eğitim227
  => Delphi Eğitim228
  => Delphi Eğitim229
  => Delphi Eğitim230
  => Delphi Eğitim231
  => Delphi Eğitim232
  => Delphi Eğitim233
  => Delphi Eğitim234
  => Delphi Eğitim235
  => Delphi Eğitim236
  => Delphi Eğitim237
  => Delphi Eğitim238
  => Delphi Eğitim239
  => Delphi Eğitim240
  => Delphi Eğitim241
  => Delphi Eğitim242
  İletişim

neoturk: ...How to search a file for a string ?...

{

  ScanFile searches for a string in a file and returns the position of the string

  in the file or -1, if not found.

 

  ScanFile sucht in einer Datei nach dem Vorkommen

  eines bestimmten Strings und gibt bei Erfolg die Position zurück, wo der String

  gefunden wurde.

}

 

function ScanFile(const FileName: string;

  const forString: string;

  caseSensitive: Boolean): Longint;

const

  BufferSize = $8001;  { 32K+1 bytes }

var

  pBuf, pEnd, pScan, pPos: PChar;

  filesize: LongInt;

  bytesRemaining: LongInt;

  bytesToRead: Integer;

  F: file;

  SearchFor: PChar;

  oldMode: Word;

begin

  { assume failure }

  Result := -1;

  if (Length(forString) = 0) or (Length(FileName) = 0) then Exit;

  SearchFor := nil;

  pBuf      := nil;

  { open file as binary, 1 byte recordsize }

  AssignFile(F, FileName);

  oldMode  := FileMode;

  FileMode := 0;    { read-only access }

  Reset(F, 1);

  FileMode := oldMode;

  try { allocate memory for buffer and pchar search string }

    SearchFor := StrAlloc(Length(forString) + 1);

    StrPCopy(SearchFor, forString);

    if not caseSensitive then  { convert to upper case }

      AnsiUpper(SearchFor);

    GetMem(pBuf, BufferSize);

    filesize       := System.Filesize(F);

    bytesRemaining := filesize;

    pPos           := nil;

    while bytesRemaining > 0 do

    begin

      { calc how many bytes to read this round }

      if bytesRemaining >= BufferSize then

        bytesToRead := Pred(BufferSize)

      else

        bytesToRead := bytesRemaining;

      { read a buffer full and zero-terminate the buffer }

      BlockRead(F, pBuf^, bytesToRead, bytesToRead);

      pEnd  := @pBuf[bytesToRead];

      pEnd^ := #0;

      pScan := pBuf;

      while pScan < pEnd do

      begin

        if not caseSensitive then { convert to upper case }

          AnsiUpper(pScan);

        pPos := StrPos(pScan, SearchFor);  { search for substring }

        if pPos <> nil then

        begin { Found it! }

          Result := FileSize - bytesRemaining +

            Longint(pPos) - Longint(pBuf);

          Break;

        end;

        pScan := StrEnd(pScan);

        Inc(pScan);

      end;

      if pPos <> nil then Break;

      bytesRemaining := bytesRemaining - bytesToRead;

      if bytesRemaining > 0 then

      begin

        Seek(F, FilePos(F) - Length(forString));

        bytesRemaining := bytesRemaining + Length(forString);

      end;

    end; { While }

  finally

    CloseFile(F);

    if SearchFor <> nil then StrDispose(SearchFor);

    if pBuf <> nil then FreeMem(pBuf, BufferSize);

  end;

end; { ScanFile }

 

 

// Search in autoexec.bat for "keyb" with case insensitive

// In der autoexec.bat nach "keyb" suchen

 

procedure TForm1.Button1Click(Sender: TObject);

var

  Position: integer;

begin

  Position := ScanFile('c:autoexec.bat', 'keyb', False);

  ShowMessage(IntToStr(Position));

end;

 

Delphi - .....................................

 

neoturk: ...How to search a file for a string ?...

{

  ScanFile searches for a string in a file and returns the position of the string

  in the file or -1, if not found.

 

  ScanFile sucht in einer Datei nach dem Vorkommen

  eines bestimmten Strings und gibt bei Erfolg die Position zurück, wo der String

  gefunden wurde.

}

 

function ScanFile(const FileName: string;

  const forString: string;

  caseSensitive: Boolean): Longint;

const

  BufferSize = $8001;  { 32K+1 bytes }

var

  pBuf, pEnd, pScan, pPos: PChar;

  filesize: LongInt;

  bytesRemaining: LongInt;

  bytesToRead: Integer;

  F: file;

  SearchFor: PChar;

  oldMode: Word;

begin

  { assume failure }

  Result := -1;

  if (Length(forString) = 0) or (Length(FileName) = 0) then Exit;

  SearchFor := nil;

  pBuf      := nil;

  { open file as binary, 1 byte recordsize }

  AssignFile(F, FileName);

  oldMode  := FileMode;

  FileMode := 0;    { read-only access }

  Reset(F, 1);

  FileMode := oldMode;

  try { allocate memory for buffer and pchar search string }

    SearchFor := StrAlloc(Length(forString) + 1);

    StrPCopy(SearchFor, forString);

    if not caseSensitive then  { convert to upper case }

      AnsiUpper(SearchFor);

    GetMem(pBuf, BufferSize);

    filesize       := System.Filesize(F);

    bytesRemaining := filesize;

    pPos           := nil;

    while bytesRemaining > 0 do

    begin

      { calc how many bytes to read this round }

      if bytesRemaining >= BufferSize then

        bytesToRead := Pred(BufferSize)

      else

        bytesToRead := bytesRemaining;

      { read a buffer full and zero-terminate the buffer }

      BlockRead(F, pBuf^, bytesToRead, bytesToRead);

      pEnd  := @pBuf[bytesToRead];

      pEnd^ := #0;

      pScan := pBuf;

      while pScan < pEnd do

      begin

        if not caseSensitive then { convert to upper case }

          AnsiUpper(pScan);

        pPos := StrPos(pScan, SearchFor);  { search for substring }

        if pPos <> nil then

        begin { Found it! }

          Result := FileSize - bytesRemaining +

            Longint(pPos) - Longint(pBuf);

          Break;

        end;

        pScan := StrEnd(pScan);

        Inc(pScan);

      end;

      if pPos <> nil then Break;

      bytesRemaining := bytesRemaining - bytesToRead;

      if bytesRemaining > 0 then

      begin

        Seek(F, FilePos(F) - Length(forString));

        bytesRemaining := bytesRemaining + Length(forString);

      end;

    end; { While }

  finally

    CloseFile(F);

    if SearchFor <> nil then StrDispose(SearchFor);

    if pBuf <> nil then FreeMem(pBuf, BufferSize);

  end;

end; { ScanFile }

 

 

// Search in autoexec.bat for "keyb" with case insensitive

// In der autoexec.bat nach "keyb" suchen

 

procedure TForm1.Button1Click(Sender: TObject);

var

  Position: integer;

begin

  Position := ScanFile('c:autoexec.bat', 'keyb', False);

  ShowMessage(IntToStr(Position));

end;

 

Delphi - .....................................

 

neoturk: ...Calculate the checksum of a file ?...

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;

 

Delphi - .....................................

 

neoturk: ...Calculate the checksum of a file ?...

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;

 

Delphi - .....................................

 

neoturk: ...Create a shortcut in the startmenu, on the desktop, ?...

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 = 'SoftwareMicroSoftWindowsCurrentVersionExplorer';

  QUICK_LAUNCH_ROOT = 'SoftwareMicroSoftWindowsCurrentVersionGrpConv';

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('SoftwareMicrosoftWindowsCurrentVersionExplorerShell 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;

 

Delphi - .....................................

 

neoturk: ...Create a shortcut in the startmenu, on the desktop, ?...

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 = 'SoftwareMicroSoftWindowsCurrentVersionExplorer';

  QUICK_LAUNCH_ROOT = 'SoftwareMicroSoftWindowsCurrentVersionGrpConv';

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('SoftwareMicrosoftWindowsCurrentVersionExplorerShell 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;

 

Delphi - .....................................

 

neoturk: ...Assign the text of a file to a string ?...

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;

 

Delphi - .....................................

 

neoturk: ...Assign the text of a file to a string ?...

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;

 

Delphi - .....................................

 

neoturk: ...Check, if two files are equal ?...

{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;

 

Delphi - .....................................

 

neoturk: ...Check, if two files are equal ?...

{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;

 

Delphi - .....................................

 

neoturk: ...Delete a file permanently ?...

{

  If you want to get rid of a file normally you just delete it.

  But someone else can undelete it if the file hasn't been wiped correctly.

  For security purposes, to insure that certain files are permanently

  gone, the WipeFile procedure writes over the data in the file with

  random characters and then erases it.

 

  Wenn man eine Datei nicht mehr braucht, löscht man sie einfach.

  Aber jemand anders kann die Datei wieder herstellen, wenn sie

  nicht "richtig" gelöscht wurde.

  Aus Sicherheitsgründen, um sicherzustellen, dass eine Datei permanent

  gelöscht wird, überschreibt die WipeFile Prozedur eine Datei mit

  Zufalls-Zeichen und löscht sie anschliessend.

}

 

procedure WipeFile(FileName: string);

var

  buffer: array [0..4095] of Byte;

  max, n: LongInt;

  i: Integer;

  fs: TFileStream;

 

  procedure RandomizeBuffer;

  var

    i: Integer;

  begin

    for i := Low(buffer) to High(buffer) do

      buffer[i] := Random(256);

  end;

begin

  fs := TFilestream.Create(FileName, fmOpenReadWrite or fmShareExclusive);

  try

    for i := 1 to 3 do

    begin

      RandomizeBuffer;

      max := fs.Size;

      fs.Position := 0;

      while max > 0 do

      begin

        if max > SizeOf(buffer) then

          n := SizeOf(buffer)

        else

          n := max;

        fs.Write(Buffer, n);

        max := max - n;

      end;

      FlushFileBuffers(fs.Handle);

    end;

  finally

    fs.Free;

  end;

  Deletefile(FileName);

end;

 

Delphi - .....................................

 

neoturk: ...Delete a file permanently ?...

{

  If you want to get rid of a file normally you just delete it.

  But someone else can undelete it if the file hasn't been wiped correctly.

  For security purposes, to insure that certain files are permanently

  gone, the WipeFile procedure writes over the data in the file with

  random characters and then erases it.

 

  Wenn man eine Datei nicht mehr braucht, löscht man sie einfach.

  Aber jemand anders kann die Datei wieder herstellen, wenn sie

  nicht "richtig" gelöscht wurde.

  Aus Sicherheitsgründen, um sicherzustellen, dass eine Datei permanent

  gelöscht wird, überschreibt die WipeFile Prozedur eine Datei mit

  Zufalls-Zeichen und löscht sie anschliessend.

}

 

procedure WipeFile(FileName: string);

var

  buffer: array [0..4095] of Byte;

  max, n: LongInt;

  i: Integer;

  fs: TFileStream;

 

  procedure RandomizeBuffer;

  var

    i: Integer;

  begin

    for i := Low(buffer) to High(buffer) do

      buffer[i] := Random(256);

  end;

begin

  fs := TFilestream.Create(FileName, fmOpenReadWrite or fmShareExclusive);

  try

    for i := 1 to 3 do

    begin

      RandomizeBuffer;

      max := fs.Size;

      fs.Position := 0;

      while max > 0 do

      begin

        if max > SizeOf(buffer) then

          n := SizeOf(buffer)

        else

          n := max;

        fs.Write(Buffer, n);

        max := max - n;

      end;

      FlushFileBuffers(fs.Handle);

    end;

  finally

    fs.Free;

  end;

  Deletefile(FileName);

end;

 

Delphi - .....................................

 

neoturk: ...Copy files to the windows clipboard ?...

uses

  ShlObj, ClipBrd;

 

procedure CopyFilesToClipboard(FileList: string);

var

  DropFiles: PDropFiles;

  hGlobal: THandle;

  iLen: Integer;

begin

  iLen := Length(FileList) + 2;

  FileList := FileList + #0#0;

  hGlobal := GlobalAlloc(GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT,

    SizeOf(TDropFiles) + iLen);

  if (hGlobal = 0) then raise Exception.Create('Could not allocate memory.');

  begin

    DropFiles := GlobalLock(hGlobal);

    DropFiles^.pFiles := SizeOf(TDropFiles);

    Move(FileList[1], (PChar(DropFiles) + SizeOf(TDropFiles))^, iLen);

    GlobalUnlock(hGlobal);

    Clipboard.SetAsHandle(CF_HDROP, hGlobal);

  end;

end;

 

// Example, Beispiel:

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  CopyFilesToClipboard('C:Bootlog.Txt'#0'C:AutoExec.Bat');

end;

 

{

  Separate the files with a #0.

  Dateien mit einem #0 trennen.

}

 

Delphi - .....................................

 

neoturk: ...Copy files to the windows clipboard ?...

uses

  ShlObj, ClipBrd;

 

procedure CopyFilesToClipboard(FileList: string);

var

  DropFiles: PDropFiles;

  hGlobal: THandle;

  iLen: Integer;

begin

  iLen := Length(FileList) + 2;

  FileList := FileList + #0#0;

  hGlobal := GlobalAlloc(GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT,

    SizeOf(TDropFiles) + iLen);

  if (hGlobal = 0) then raise Exception.Create('Could not allocate memory.');

  begin

    DropFiles := GlobalLock(hGlobal);

    DropFiles^.pFiles := SizeOf(TDropFiles);

    Move(FileList[1], (PChar(DropFiles) + SizeOf(TDropFiles))^, iLen);

    GlobalUnlock(hGlobal);

    Clipboard.SetAsHandle(CF_HDROP, hGlobal);

  end;

end;

 

// Example, Beispiel:

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  CopyFilesToClipboard('C:Bootlog.Txt'#0'C:AutoExec.Bat');

end;

 

{

  Separate the files with a #0.

  Dateien mit einem #0 trennen.

}

 

Delphi - .....................................

 

neoturk: ...Rename a directory ?...

uses

  ShellApi;

 

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.Button1Click(Sender: TObject);

begin

  RenameDir('C:Dir1', 'C:Dir2');

end;

 

Delphi - .....................................

 

neoturk: ...Rename a directory ?...

uses

  ShellApi;

 

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.Button1Click(Sender: TObject);

begin

  RenameDir('C:Dir1', 'C:Dir2');

end;

 

Delphi - .....................................

 

neoturk: ...Retrieve a shortcut's link information ?...

uses

  ShlObj,

  ComObj,

  ActiveX,

  CommCtrl;

 

type

  PShellLinkInfoStruct = ^TShellLinkInfoStruct;

  TShellLinkInfoStruct = record

    FullPathAndNameOfLinkFile: array[0..MAX_PATH] of Char;

    FullPathAndNameOfFileToExecute: array[0..MAX_PATH] of Char;

    ParamStringsOfFileToExecute: array[0..MAX_PATH] of Char;

    FullPathAndNameOfWorkingDirectroy: array[0..MAX_PATH] of Char;

    Description: array[0..MAX_PATH] of Char;

    FullPathAndNameOfFileContiningIcon: array[0..MAX_PATH] of Char;

    IconIndex: Integer;

    HotKey: Word;

    ShowCommand: Integer;

    FindData: TWIN32FINDDATA;

  end;

 

procedure GetLinkInfo(lpShellLinkInfoStruct: PShellLinkInfoStruct);

var

  ShellLink: IShellLink;

  PersistFile: IPersistFile;

  AnObj: IUnknown;

begin

  // access to the two interfaces of the object

  AnObj       := CreateComObject(CLSID_ShellLink);

  ShellLink   := AnObj as IShellLink;

  PersistFile := AnObj as IPersistFile;

 

  // Opens the specified file and initializes an object from the file contents.

  PersistFile.Load(PWChar(WideString(lpShellLinkInfoStruct^.FullPathAndNameOfLinkFile)), 0);

  with ShellLink do

  begin

    // Retrieves the path and file name of a Shell link object.

    GetPath(lpShellLinkInfoStruct^.FullPathAndNameOfFileToExecute,

      SizeOf(lpShellLinkInfoStruct^.FullPathAndNameOfLinkFile),

      lpShellLinkInfoStruct^.FindData,

      SLGP_UNCPRIORITY);

 

    // Retrieves the description string for a Shell link object.

    GetDescription(lpShellLinkInfoStruct^.Description,

      SizeOf(lpShellLinkInfoStruct^.Description));

 

    // Retrieves the command-line arguments associated with a Shell link object.

    GetArguments(lpShellLinkInfoStruct^.ParamStringsOfFileToExecute,

      SizeOf(lpShellLinkInfoStruct^.ParamStringsOfFileToExecute));

 

    // Retrieves the name of the working directory for a Shell link object.

    GetWorkingDirectory(lpShellLinkInfoStruct^.FullPathAndNameOfWorkingDirectroy,

      SizeOf(lpShellLinkInfoStruct^.FullPathAndNameOfWorkingDirectroy));

 

    // Retrieves the location (path and index) of the icon for a Shell link object.

    GetIconLocation(lpShellLinkInfoStruct^.FullPathAndNameOfFileContiningIcon,

      SizeOf(lpShellLinkInfoStruct^.FullPathAndNameOfFileContiningIcon),

      lpShellLinkInfoStruct^.IconIndex);

 

    // Retrieves the hot key for a Shell link object.

    GetHotKey(lpShellLinkInfoStruct^.HotKey);

 

    // Retrieves the show (SW_) command for a Shell link object.

    GetShowCmd(lpShellLinkInfoStruct^.ShowCommand);

  end;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

const

  br = #13#10;

var

  LinkInfo: TShellLinkInfoStruct;

  s: string;

begin

  FillChar(LinkInfo, SizeOf(LinkInfo), #0);

  LinkInfo.FullPathAndNameOfLinkFile := 'C:WINNTProfilesuserDesktopFileName.lnk';

  GetLinkInfo(@LinkInfo);

  with LinkInfo do

    s := FullPathAndNameOfLinkFile + br +

      FullPathAndNameOfFileToExecute + br +

      ParamStringsOfFileToExecute + br +

      FullPathAndNameOfWorkingDirectroy + br +

      Description + br +

      FullPathAndNameOfFileContiningIcon + br +

      IntToStr(IconIndex) + br +

      IntToStr(LoByte(HotKey)) + br +

      IntToStr(HiByte(HotKey)) + br +

      IntToStr(ShowCommand) + br +

      FindData.cFileName + br +

      FindData.cAlternateFileName;

  Memo1.Lines.Add(s);

end;

 

// Only for D3 or higher.

// for D1,D2 users: http://www.hitekdev.com/delphi/shellutlexamples.html

 

Delphi - .....................................

 

neoturk: ...Retrieve a shortcut's link information ?...

uses

  ShlObj,

  ComObj,

  ActiveX,

  CommCtrl;

 

type

  PShellLinkInfoStruct = ^TShellLinkInfoStruct;

  TShellLinkInfoStruct = record

    FullPathAndNameOfLinkFile: array[0..MAX_PATH] of Char;

    FullPathAndNameOfFileToExecute: array[0..MAX_PATH] of Char;

    ParamStringsOfFileToExecute: array[0..MAX_PATH] of Char;

    FullPathAndNameOfWorkingDirectroy: array[0..MAX_PATH] of Char;

    Description: array[0..MAX_PATH] of Char;

    FullPathAndNameOfFileContiningIcon: array[0..MAX_PATH] of Char;

    IconIndex: Integer;

    HotKey: Word;

    ShowCommand: Integer;

    FindData: TWIN32FINDDATA;

  end;

 

procedure GetLinkInfo(lpShellLinkInfoStruct: PShellLinkInfoStruct);

var

  ShellLink: IShellLink;

  PersistFile: IPersistFile;

  AnObj: IUnknown;

begin

  // access to the two interfaces of the object

  AnObj       := CreateComObject(CLSID_ShellLink);

  ShellLink   := AnObj as IShellLink;

  PersistFile := AnObj as IPersistFile;

 

  // Opens the specified file and initializes an object from the file contents.

  PersistFile.Load(PWChar(WideString(lpShellLinkInfoStruct^.FullPathAndNameOfLinkFile)), 0);

  with ShellLink do

  begin

    // Retrieves the path and file name of a Shell link object.

    GetPath(lpShellLinkInfoStruct^.FullPathAndNameOfFileToExecute,

      SizeOf(lpShellLinkInfoStruct^.FullPathAndNameOfLinkFile),

      lpShellLinkInfoStruct^.FindData,

      SLGP_UNCPRIORITY);

 

    // Retrieves the description string for a Shell link object.

    GetDescription(lpShellLinkInfoStruct^.Description,

      SizeOf(lpShellLinkInfoStruct^.Description));

 

    // Retrieves the command-line arguments associated with a Shell link object.

    GetArguments(lpShellLinkInfoStruct^.ParamStringsOfFileToExecute,

      SizeOf(lpShellLinkInfoStruct^.ParamStringsOfFileToExecute));

 

    // Retrieves the name of the working directory for a Shell link object.

    GetWorkingDirectory(lpShellLinkInfoStruct^.FullPathAndNameOfWorkingDirectroy,

      SizeOf(lpShellLinkInfoStruct^.FullPathAndNameOfWorkingDirectroy));

 

    // Retrieves the location (path and index) of the icon for a Shell link object.

    GetIconLocation(lpShellLinkInfoStruct^.FullPathAndNameOfFileContiningIcon,

      SizeOf(lpShellLinkInfoStruct^.FullPathAndNameOfFileContiningIcon),

      lpShellLinkInfoStruct^.IconIndex);

 

    // Retrieves the hot key for a Shell link object.

    GetHotKey(lpShellLinkInfoStruct^.HotKey);

 

    // Retrieves the show (SW_) command for a Shell link object.

    GetShowCmd(lpShellLinkInfoStruct^.ShowCommand);

  end;

end;

 

procedure TForm1.Button1Click(Sender: TObject);

const

  br = #13#10;

var

  LinkInfo: TShellLinkInfoStruct;

  s: string;

begin

  FillChar(LinkInfo, SizeOf(LinkInfo), #0);

  LinkInfo.FullPathAndNameOfLinkFile := 'C:WINNTProfilesuserDesktopFileName.lnk';

  GetLinkInfo(@LinkInfo);

  with LinkInfo do

    s := FullPathAndNameOfLinkFile + br +

      FullPathAndNameOfFileToExecute + br +

      ParamStringsOfFileToExecute + br +

      FullPathAndNameOfWorkingDirectroy + br +

      Description + br +

      FullPathAndNameOfFileContiningIcon + br +

      IntToStr(IconIndex) + br +

      IntToStr(LoByte(HotKey)) + br +

      IntToStr(HiByte(HotKey)) + br +

      IntToStr(ShowCommand) + br +

      FindData.cFileName + br +

      FindData.cAlternateFileName;

  Memo1.Lines.Add(s);

end;

 

// Only for D3 or higher.

// for D1,D2 users: http://www.hitekdev.com/delphi/shellutlexamples.html

 

Delphi - .....................................

 

neoturk: ...Read a binary file and display the byte values as ascii ?...

type

  TDisplayProc = procedure(const s: string) of object;

 

procedure ShowBinary(var Data; Count: Cardinal; DispProc: TDisplayProc);

 

implementation

 

 

procedure ShowBinary(var Data; Count: Cardinal; DispProc: TDisplayProc);

var

  line: string[80];

  i: Cardinal;

  p: PChar;

  nStr: string[4];

const

  posStart = 1;

  binStart = 7;

  ascStart = 57;

  HexChars: PChar = '0123456789ABCDEF';

begin

  p    := @Data;

  line := '';

  for i := 0 to Count - 1 do

  begin

    if (i mod 16) = 0 then

    begin

      if Length(line) > 0 then

        DispProc(line);

      FillChar(line, SizeOf(line), ' ');

      line[0] := Chr(72);

      nStr    := Format('%4.4X', [i]);

      Move(nStr[1], line[posStart], Length(nStr));

      line[posStart + 4] := ':';

    end;

    if p[i] >= ' ' then

      line[i mod 16 + ascStart] := p[i]

    else

      line[i mod 16 + ascStart] := '.';

    line[binStart + 3 * (i mod 16)]     := HexChars[(Ord(p[i]) shr 4) and $F];

    line[binStart + 3 * (i mod 16) + 1] := HexChars[Ord(p[i]) and $F];

  end;

  DispProc(line);

end;

 

 

procedure TForm1.Display(const S: string);

begin

  Memo1.Lines.Add(S);

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

  ms: TMemoryStream;

begin

  if Opendialog1.Execute then

  begin

    ms := TMemoryStream.Create;

    try

      ms.LoadFromfile(OpenDialog1.FileName);

      ShowBinary(ms.Memory^, ms.Size, Display);

    finally

      ms.Free

    end;

  end;

end;

Bu web sitesi ücretsiz olarak Bedava-Sitem.com ile oluşturulmuştur. Siz de kendi web sitenizi kurmak ister misiniz?
Ücretsiz kaydol