1. Home
  2. Computing & Technology
  3. Delphi Programming
Hosts Editor
Free source rapid access "hosts" file editor - easily manage mappings of IPs to Host names or URL's.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 More of this Feature
• DOWNLOAD CODE
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• How to get your own IP
• API Programming
• Running with parameters

Article submitted by Nicolae Bogdan for the Delphi Programming Quickies Contest.

Hosts
Many administrators need to have better control when configuring workstations. A method of increasing network performance is through the file called "hosts" which keeps mappings of IP addresses to host names or URL's. On Windows XP systems, this file is located under "\Windows\System32\Drivers\Etc". To manage the "hosts" file, if it isn't already available on the computer, you have to create it and edit manually. My simple program automates completely this task with detection of the existence of the "hosts" file.

I have included 2 versions: one includes confirmation dialogs at start, open file, create file, user exit; the other one displays a dialog only when you need to create the hosts file. This program works from local workstation on Windows platforms.
If you use a share name as the first parameter you can modify the hosts file of another computer which is very useful. Be warned that you need write permissions to be able to modify a file on another computer.

Hosts file

Download FULL code, code sample:

begin
{$I-}
reset(f);
closefile(f);
{$I+}
 if ioresult  <> 0 then
  if MessageBox
        (0, 
        pchar('Create "' + s2 + '" file?'),
        'Create "hosts" file ?',
        mb_yesno)=6 then 
  begin 
    rewrite(f);
    closefile(f);
    ShellExecute(0,
                 'open',
                 pchar(s1),
                 pchar(s2), 
                 nil, 
                 1);
     if (GetWinVersion=wvWin95) or 
        (GetWinVersion=wvWin98) then
        ShellExecute(0,
                     'open',
                     pchar(s1),
                     pchar(windir+'\hosts'), 
                     nil, 1);
  end
  else 
    MessageBox(0,
               'File doesn''t exists! ',
               'File "hosts" not created',
               mb_ok)
 else
   ShellExecute(0,
                'open',
                pchar(s1),
                pchar(s2), 
                nil, 
                1) 
end

Basically, this is entry level Delphi programming. The program's size is optimized according to the article: "Your first raw API Delphi program" and "Get Windows version". I hope this program will be helpful for true beginners to understand Delphi/Pascal instructions.

Don't miss: Delphi Programming Quickies Contest!

Explore Delphi Programming
About.com Special Features

Reader's Choice Award Winners

What are the best instant messengers, apps, editors and more? You told us, for our 2010 technology awards program. More >

iPad Central

Is Apple's new tablet computer impractical, a must-have -- or both? We'll help you figure it out. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2010 About.com, a part of The New York Times Company.

All rights reserved.