I have an application having 3 Forms (TForm1, TForm2, TForm3). I need the code for the following : On TForm1.BitBtn Click "10.220.70.32 BSNLESDP25A" and "10.220.70.33 BSNLESDP25B" will be searched from "host" file located in "%windir%\System32\drivers\etc" directory. If found "host" file attributes will be changed to "Readonly" and "System" and Form2 will be shown. If not found then "Readonly" and "System" attributes of "host" file will be removed and two lines will be appended to "host" file as "10.220.70.32 BSNLESDP25A" and "10.220.70.33 BSNLESDP25B" and Form3 will be shown.
Asked
Active
Viewed 760 times
1
-
4Please post code showing what you've tried so far, and explain how it isn't working the way you expected. This isn't a site where people write all your code for you; you need to show you've put some effort into solving the problem yourself. – Ken White Dec 26 '11 at 19:10
-
I am learner. I have not clearcut idea. – Koushik Halder Dec 26 '11 at 19:12
-
You will need to run the process elevated and deal with file redirection on 64 bit systems. Changing attributes seems pointless. What are trying to achieve? – David Heffernan Dec 27 '11 at 09:28
1 Answers
2
You can use IOUtils.TFile for the GetAttributes
and SetAttributes
; here's an example from the XE2 documentation that shows using both.
Since the hosts file is usually pretty small, though, I'd probably use TStringList
to open and search it, as it's the quickest and easiest way.
uses
System.IOUtils;
// Clear the readonly and system attributes
var
Attributes: TFileAttributes;
SL: TStringList;
Idx: Integer;
begin
Attributes := []; // Clear any existing attributes
TFile.SetAttributes(PathAndFileName, Attributes);
SL := TStringList.Create;
try
SL.LoadFromFile(PathAndFileName);
if SL.IndexOf(YourFirstSearchString) = -1 then // Not found
SL.Add(YourFirstSearchString);
if SL.IndexOf(YourSecondSearchString) = -1 then
SL.Add(YourSecondSearchString);
SL.SaveToFile(PathAndFileName);
finally
SL.Free;
end;
Include(Attributes, TFileAttribute.faSystem);
Include(Attributes, TFileAttribute.faReadOnly);
TFile.SetAttributes(PathAndFileName, Attributes);
end;
Note you'll have problems doing this without running under an Administrator account, as nothing in the Windows\
folder can be written to otherwise. You should include a manifest in your application that tells Windows the app requires administrator rights, so UAC will prompt the user for an administrator account and password. There are examples of adding the manifest here on SO.
(Also see David's comment to your question about redirection on 64-bit Windows.)

Ken White
- 123,280
- 14
- 225
- 444
-
"Attributes: TFileAttributes" gives undefined Identifier. Please help me. – Koushik Halder Dec 31 '11 at 19:10
-
-
Sorry, just remembered you mentioned XE2. I've fixed the code to reflect the new naming requirements. – Ken White Dec 31 '11 at 20:18
-
If I use "%windir%\System32\drivers\etc\host" as "PathAndFileName", it gives error but in case of "C:\WINDOWS\system32\drivers\etc\hosts", there is no error. I wish to use "%windir%\System32\drivers\etc\host" as "PathAndFileName" because in some cases Windows may be installed in other drive like "D" or "E" drive. Please tell me how it can be defined. Please.. – Koushik Halder Jan 02 '12 at 19:10
-
That's a separate topic. Please post a new question and ask it there. – Ken White Jan 02 '12 at 19:13
-
If I try to remove the string using the following code it gives error. The code is : SL.LoadFromFile(PathAndFileName); if SL.IndexOf(YourFirstSearchString) <> -1 then SL.Delete (YourFirstSearchString); – Koushik Halder Jan 11 '12 at 13:51
-
"It gives error" is meaningless, and this would again be a separate question. Please post it as such, and explain the **exact** error you're getting, complete with the precise error message and any memory addresses it contains. "It gives error" is like telling your mechanic "My car doesn't work." with no more details; do you expect your car to be repaired from that bit of information? We can't see your screen or read your mind to find out what you mean - you have to tell us precisely. – Ken White Jan 11 '12 at 16:51