Open Host File and add entry using VB6

Using VB6 I would like to block websites using the hosts file located on a PC.  This requires that I open Notepad as Administrator and then open up the hosts.txt file from Notepad.  Attached is an example of the hosts file that I need to open.  As you can see, it has only comments in the file.  However, to add a blocked site, requires adding the following line at the end.

The IP address will always be the same.  The blocked domain name comes after the address.

Typically the hosts file is located at c:\windows\system32\drivers\etc    The only problem is that it can only be edited in administrator mode.  So I would have to open NotePad as administrator and then open the file from Notepad in order for this to work.  Unless you can think of any other ideas to do this.

Attached is a previous project in which this was done with a simalar file.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
I'm not familar with vb. Still, if there is a function you can use to run a shell, and if you are open to using something like Microsoft's psexec, you may be able run something like this from the shell to add the line to hosts.:
psexec -s "cmd /c echo>> c:\windows\system32\drivers\etc\hosts"

Open in new window

The -s switch tells psexec to run as the System account.
Also, this assumes the hosts file is editable and not locked by another program such as antivirus, which may prevent hosts from being edited.

As an alternative to the -s switch, you can use -u and -p switches to pass the admin name and password.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
First of all: This does not block websites at all. It just disables the default DNS lookup. So it's a weird case of obscurity.

The only way to block a website is to block all IP addresses where you can obtain it. Here you may use the local Windows Fire wall.

But this is better done via GPO.
al4629740Author Commented:
Yes but I am trying to do this via DNS with the host file
ste5anSenior DeveloperCommented:
It's like comparing apples and oranges..
ste5anSenior DeveloperCommented:
Just to be clear: It's NOT POSSIBLE to block a web site with a host file entry..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.