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

a1
a2
a3
a4
a5

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

C:\WINDOWS\system32\drivers\etc

and add the entry to the hosts file

such as

vv.xx.yy.zz          a1.go.domain.com
vv1.xx1.yy1.zz1   a1.see.domain.com

similarly if user clicks on a2 and hits execute


vv.xx.yy.zz          a1.go.domain.com
vv1.xx1.yy1.zz1   a1.see.domain.com


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  

vv.xx.yy.zz          a1.go.domain.com
vv1.xx1.yy1.zz1   a1.see.domain.com


to the hosts file

close the file.


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









VlearnsAsked:
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.

JR2003Commented:
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          a1.go.domain.com"
        .WriteLine "vv1.xx1.yy1.zz1 a1.see.domain.com"
        .Close
    End With
End Sub
0

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

Dim sSelectedItem as String
sSelectedItem  = Combo1.List(Combo1.ListIndex)
0
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


0
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

at

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

shouldnt fso be initialized to somethin?
0
VlearnsAuthor Commented:

solved the problem:

should have been

Dim fso As New Scripting.FileSystemObject
instead ogf

Dim fso As Scripting.FileSystemObject


0
VlearnsAuthor Commented:

can someone tell me how to write this to file

.WriteLine "123.111.222.333  & Item & .domain.com "

where the output should be

123.111.222.333  ab.domain.com


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


0
JR2003Commented:
<<<<can someone tell me how to write this to file  .WriteLine "123.111.222.333  & Item & .domain.com "

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