How to Add an entry to the host file from VB.Net

Posted on 2014-08-21
Last Modified: 2014-08-24
I need to add an entry to the host file from a program that i am writing in VB.Net. I have tried to change the code in the following post but can’t get it to add rather than edit. Can someone please help me as I am running out of time to get the program finished.
All help will be greatly appreciated
Question by:JNeets
    LVL 35

    Accepted Solution

    See output line, that's the most important one to watch (I just added something random):

            Dim Hosts As String = Environment.SystemDirectory & "\drivers\etc\hosts"
            Dim sr As New StreamReader(Hosts)
            Dim input As String = sr.ReadToEnd()
            Dim output As String = input + vbNewLine + " router"
            Dim sw As New StreamWriter(Hosts)

    Open in new window

    LVL 74

    Assisted Solution

    by:käµfm³d 👽
    If it's not obvious, since the hosts file is in a protected system directory, any application that modifies it will need to be running with administrator privileges.

    Author Closing Comment

    Thanks Kimputer for the code I tryed it out and it worked purfectly. also thanks Kaufmed for the reminder that the program will have to run with administrator privileges.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now