Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7007347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:35:44+00:00 2026-05-27T21:35:44+00:00

I have an application that will work with the HOSTS file in the Windows\System32\drivers\etc

  • 0

I have an application that will work with the HOSTS file in the Windows\System32\drivers\etc folder. However, I don’t want to hard code the path to C:\Windows\System32, because Windows might not be installed on drive C:.

I tried using %WinDir%\system32\drivers\etc\hosts, but this doesn’t get expanded when it’s used in the variable in my code.

How can I use %WinDir%\system32\drivers\etc\hosts as the path to the hosts file so I don’t have to hard-code the path?

Another problem is that on successful compilation I have received one warning as

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

Code is shown in the answer here

Here is my new code :

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:35:45+00:00Added an answer on May 27, 2026 at 9:35 pm

    Don’t use the %Windir%\System32. Use the Windows API’s function designed specifically to find that folder, GetSystemDirectory. It’s defined in the Windows unit; here’s a quick wrapper (not tested on XE2, but works on XE):

    Since you had problems with my previous answer, here’s a fully-compiling copy of the code (I commented out your references to Edit02, so you’ll need to uncomment them; everything else compiles just fine as is under 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}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that will mainly operate in the background. I want certain
We have an application that will be collecting data and storing it in local
I have an application that will accept URLs from the built in web browser
I have this application that will recurse all folders in a given directory and
I have an application that requires resizing of a component that will be scaled
I have a console application that will be kicked off with a scheduler. If
I have a PHP application that will on occasion have to handle URLs where
I have a desktop application that will 'sync' with an iPhone application. The sync
I have a web application that will need to access code behind methods as
I have DLL and application that will call some function in this dll. For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.