Windows目录定义

Kou*_*der 2 delphi delphi-xe2

我有一个应用程序,可以使用该HOSTS文件Windows\System32\drivers\etc夹中的文件.但是,我不想硬编码路径C:\Windows\System32,因为Windows可能没有安装在驱动器C:上.

我尝试过使用%WinDir%\system32\drivers\etc\hosts,但是当我在代码中的变量中使用它时,它不会被扩展.

我如何使用文件%WinDir%\system32\drivers\etc\hosts的路径,hosts所以我不必硬编码路径?

另一个问题是,在成功编译时,我收到了一个警告

[DCC警告] ApplicationWizard01.pas(67):W1002符号'TFileAttributes'特定于平台.

代码显示在这里的答案中

这是我的新代码:

unit KoushikHalder01;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
  Vcl.ComCtrls;
type
  TForm01 = class(TForm)
    Label01: TLabel;
    Edit01: TEdit;
    Edit02: TEdit;
    BitBtn01: TBitBtn;
    BitBtn02: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure BitBtn01MouseEnter(Sender: TObject);
    procedure BitBtn02MouseEnter(Sender: TObject);
    procedure BitBtn01MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn02MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure BitBtn01MouseLeave(Sender: TObject);
    procedure BitBtn02MouseLeave(Sender: TObject);
    procedure BitBtn02Click(Sender: TObject);
    procedure BitBtn01Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form01: TForm01;

implementation

{$R *.dfm}

uses System.IOUtils;

procedure TForm01.BitBtn01Click(Sender: TObject);
function GetSysDir: string;
function IncludeTrailingPathDelimiter(const S: string): string;
var
  Attributes: TFileAttributes;
  SL: TStringList;
  Idx: Integer;
  Buffer: array[0..MAX_PATH] of Char;
  PathAndFileName : String;
begin
   GetSystemDirectory(Buffer, MAX_PATH - 1);
   SetLength(Result, StrLen(Buffer));
   Result := Buffer;
   PathAndFileName := CheckTrailingPathDelimiter(GetSysDir) + 'drivers\etc\hosts`;
   Attributes := [];
   TFile.SetAttributes('PathAndFileName', Attributes);
   SL := TStringList.Create;
   try
      SL.LoadFromFile('PathAndFileName');

     if
        SL.IndexOf('10.220.70.34    VIRTSDP25') <> -1
     then
        begin
        Edit02.Font.Color := clRed;
        Edit02.Text := 'Your Host File Has Already Been Modified Successfully.';
        end;
     if
        SL.IndexOf('10.220.70.34    VIRTSDP25') = -1
     then
        begin
        SL.Add('10.220.70.34    VIRTSDP25');
        Edit02.Font.Color := clGreen;
        Edit02.Text := 'Your Host File Has Been Modified Successfully.';
        end;
     if
        SL.IndexOf('10.220.70.32    BSNLESDP25A') = -1
     then
        SL.Add('10.220.70.32    BSNLESDP25A');
     if
        SL.IndexOf('10.220.70.33    BSNLESDP25B') = -1
     then
        SL.Add('10.220.70.33    BSNLESDP25B');
     if
        SL.IndexOf('10.220.70.34    VIRTBSNLESDP25') = -1
     then
        SL.Add('10.220.70.34    VIRTBSNLESDP25');
     if
        SL.IndexOf('10.220.70.34    KOSDPTwentyfive.bsnl.in.net') = -1
     then
        SL.Add('10.220.70.34    KOSDPTwentyfive.bsnl.in.net');
     if
        SL.IndexOf('10.220.70.34    KOSDPTwentyfive.bsnl.net.in') = -1
     then
        begin
           SL.Add('10.220.70.34 KOSDPTwentyfive.bsnl.net.in');
           SL.SaveToFile('PathAndFileName');
        end;
     finally
       SL.Free;
   end;
    Include(Attributes, TFileAttribute.faSystem);
    Include(Attributes, TFileAttribute.faReadOnly);
    TFile.SetAttributes('PathAndFileName', Attributes);
end;
Run Code Online (Sandbox Code Playgroud)

Ken*_*ite 5

不要使用%Windir%\System32.使用专门设计的Windows API函数来查找该文件夹GetSystemDirectory.它是在Windows单元中定义的; 这是一个快速包装器(未在XE2上测试,但适用于XE):

既然你与我以前的答案的问题,这里的代码的完全拷贝编译(我评论了你的引用Edit02,所以你必须解除他们,一切编译就好如同XE2下:

uses 
  System.IOUtils;

function GetSysDir: string;
var
  Buffer: array[0..MAX_PATH] of Char;
begin
   GetSystemDirectory(Buffer, MAX_PATH - 1);
   SetLength(Result, StrLen(Buffer));
   Result := Buffer;
end;

{$WARN SYMBOL_PLATFORM OFF}
procedure TForm2.Button1Click(Sender: TObject);
var
  Attributes: TFileAttributes;
  SL: TStringList;
  Idx: Integer;
  PathAndFileName : String;
begin
   PathAndFileName := IncludeTrailingPathDelimiter(GetSysDir) + 'drivers\etc\hosts';
   Attributes := [];
   TFile.SetAttributes(PathAndFileName, Attributes);
   SL := TStringList.Create;
   try
     SL.LoadFromFile(PathAndFileName);

     if SL.IndexOf('10.220.70.34    VIRTSDP25') <> -1 then
     begin
       //Edit02.Font.Color := clRed;
       //Edit02.Text := 'Your Host File Has Already Been Modified Successfully.';
     end;

     if SL.IndexOf('10.220.70.34    VIRTSDP25') = -1 then
     begin
       SL.Add('10.220.70.34    VIRTSDP25');
       //Edit02.Font.Color := clGreen;
       //Edit02.Text := 'Your Host File Has Been Modified Successfully.';
     end;

     if SL.IndexOf('10.220.70.32    BSNLESDP25A') = -1 then
       SL.Add('10.220.70.32    BSNLESDP25A');
     if SL.IndexOf('10.220.70.33    BSNLESDP25B') = -1 then
       SL.Add('10.220.70.33    BSNLESDP25B');
     if SL.IndexOf('10.220.70.34    VIRTBSNLESDP25') = -1 then
       SL.Add('10.220.70.34    VIRTBSNLESDP25');
     if SL.IndexOf('10.220.70.34    KOSDPTwentyfive.bsnl.in.net') = -1 then
       SL.Add('10.220.70.34    KOSDPTwentyfive.bsnl.in.net');
     if SL.IndexOf('10.220.70.34    KOSDPTwentyfive.bsnl.net.in') = -1 then
       SL.Add('10.220.70.34 KOSDPTwentyfive.bsnl.net.in');
     SL.SaveToFile(PathAndFileName);
   finally
       SL.Free;
   end;
    Include(Attributes, TFileAttribute.faSystem);
    Include(Attributes, TFileAttribute.faReadOnly);
    TFile.SetAttributes(PathAndFileName, Attributes);
end;
{$WARN SYMBOL_PLATFORM ON}
Run Code Online (Sandbox Code Playgroud)