simple question

i am new to vb:

this is what i want to do

i have a combobox with a list of items:
each of these represent domain names


i also have a command button called  'execute'

now when user selects on either of this , say a1, and hits 'execute' my application needs to go to


and add the entry to the hosts file

such as


similarly if user clicks on a2 and hits execute


i now the ip addresses vv.xx.yy.zz etc, so i need to know how to do file handling in vb.

this is what is intend to do...

when user selects one of a1,a2 etc,

backuo hosts to host.bak
delete hosts
create a new file 'hosts' using vb
programmatically write  


to the hosts file

close the file.

please guide me how i can achieve this VB programmatically. i am not familiarwith VB programming constructs.

Who is Participating?
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.

Include a reference to the "Microsoft Scripting Runtime" in your Project\References

Private Sub Command1_Click()
    Dim fso As Scripting.FileSystemObject
    Dim MyFile As Scripting.TextStream
    With fso
        'Back up the file
        .CopyFile "C:\WINDOWS\system32\drivers\etc\hosts.txt", "C:\WINDOWS\system32\drivers\etc\hosts.bak", True
        Set MyFile = .OpenTextFile("C:\WINDOWS\system32\drivers\etc\hosts.txt", ForAppending, True)
    End With
    With MyFile
        .WriteLine "vv.xx.yy.zz"
        .WriteLine "vv1.xx1.yy1.zz1"
    End With
End Sub

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
To access the values in the combobox:

Dim sSelectedItem as String
sSelectedItem  = Combo1.List(Combo1.ListIndex)
VlearnsAuthor Commented:
the hosts file should be just 'hosts' and not hosts.txt
do you think the opentextfile can only create the hosts file with hosts.txt?
i will try and see

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

VlearnsAuthor Commented:
got this error

Object Variable or With Block Variable not set


.CopyFile "C:\WINDOWS\system32\drivers\etc\hosts", "C:\WINDOWS\system32\drivers\etc\hosts.bak", True

shouldnt fso be initialized to somethin?
VlearnsAuthor Commented:

solved the problem:

should have been

Dim fso As New Scripting.FileSystemObject
instead ogf

Dim fso As Scripting.FileSystemObject

VlearnsAuthor Commented:

can someone tell me how to write this to file

.WriteLine "  & Item & "

where the output should be

where Item="ab"  ????????????????????????

<<<<can someone tell me how to write this to file  .WriteLine "  & Item & "

.WriteLine " "  &  Combo1.List(Combo1.ListIndex) & ""
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.