gpt4 book ai didi

delphi - Windows目录定义

转载 作者:行者123 更新时间:2023-12-02 05:13:17 30 4
gpt4 key购买 nike

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

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

如何使用 %WinDir%\system32\drivers\etc\hosts 作为 hosts 文件的路径,这样我就不必对路径?

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

[DCC Warning] ApplicationWizard01.pas(67): W1002 Symbol 'TFileAttributes' is specific to a platform.

代码显示在答案here

这是我的新代码:

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;

最佳答案

不要使用%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}

关于delphi - Windows目录定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8716447/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com