2007. december 31., hétfő

How to use the Wininit.ini to delete files on startup


Problem/Question/Abstract:

Can anyone tell me how to delete several files using wininit.ini please?I've seen an example somewhere that included the following :

[Rename]
NULL=C:\temp\readme.txt

Using the regular inifile calls, I cant use the above method for deleting several files because each WriteString would overwrite previous "NULL=" entries. I'm unable to find any info about using wininit.ini anywhere, there might be a [delete] section for all I know.

Answer:

This will do the job:

procedure DeleteAtReboot(FileList: TStringList);
var
  SList: TStringList;
  szContents: string;
  i, SectionFoundIndex: Integer;
  WinDir: array[0..MAX_PATH] of char;
  WinFile: string;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    {Use MoveFileEx}
    for i := 0 to FileList.count - 1 do
      MoveFileEx(PChar(FileList[i]), nil, MOVEFILE_DELAY_UNTIL_REBOOT);
  end
  else
  begin
    GetWindowsDirectory(WinDir, MAX_PATH);
    WinFile := IncludeTrailingBackslash(WinDir) + 'Wininit.ini';
    SList := TStringList.Create;
    try
      SectionFoundIndex := -1;
      {Load it if it exists}
      if FileExists(WinFile) then
        SList.LoadFromFile(WinFile);
      for i := 0 to SList.Count - 1 do
      begin
        szContents := uppercase(SList[i]);
        if UpperCase(SList[i]) = '[RENAME]' then
        begin
          SectionFoundIndex := i;
          break;
        end;
      end;
      {Rename Section doesn't exist...}
      if SectionFoundIndex = -1 then
        SectionFoundIndex := SList.Add('[Rename]');
      {Now Add our Files}
      for i := 0 to FileList.count - 1 do
        SList.Insert(SectionFoundIndex + 1, 'NUL=' + FileList[i]);
      SList.SaveToFile(WinFile);
    finally
      SList.Free;
    end;
  end;
end;

Nincsenek megjegyzések:

Megjegyzés küldése