Writing to a .htm file

I have made a script which creates folders, for each folder it inserts an index.htm file.

This is working fine, but after i create the index.htm file and try to open it to write text to it, it says it cannot open the speciifed folder because it is being used by a different process.

here is my short code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim counties(69) As String
        Dim i As Integer
        Dim oFile As System.IO.File
        Dim oWrite As System.IO.StreamWriter
        Dim textforindex As String
        Dim oRead As System.IO.StreamReader
        textforindex = "This is Test Text"
        counties(0) = "Aberdeenshire"
        counties(1) = "Anglesey"
        counties(2) = "Angus"
        counties(3) = "Argyle"
        counties(4) = "Avon"
        counties(5) = "Ayrshire"
        counties(6) = "Bedfordshire"
        counties(7) = "Berkshire"
        counties(8) = "Birmingham"
        counties(9) = "Buckinghamshire"
        counties(10) = "Cambridgeshire"
        counties(11) = "Carmarthenshire"
        counties(12) = "Ceredigion"
        counties(13) = "Cheshire"
        counties(14) = "Conwy"
        counties(15) = "Cornwall"
        counties(16) = "Cumbria"
        counties(17) = "Denbighshire"
        counties(18) = "Derbyshire"
        counties(19) = "Devon"
        counties(20) = "Dorset"
        counties(21) = "Durham"
        counties(22) = "Edinburgh"
        counties(23) = "Essex"
        counties(24) = "Fife"
        counties(25) = "Flintshire"
        counties(26) = "Gloucestershire"
        counties(27) = "Gwynedd"
        counties(28) = "Hampshire"
        counties(29) = "Herefordshire"
        counties(30) = "Hertfordshire"
        counties(31) = "Highland"
        counties(32) = "Kent"
        counties(33) = "Lanarkshire"
        counties(34) = "Leicestershire"
        counties(35) = "Linconshire"
        counties(36) = "London"
        counties(37) = "Manchester"
        counties(38) = "Merseyside"
        counties(39) = "Middlesex"
        counties(40) = "Moray"
        counties(41) = "Norfolk"
        counties(42) = "Northamptonshire"
        counties(43) = "Northumberland"
        counties(44) = "Nottinghamshire"
        counties(45) = "Oxfordshire"
        counties(46) = "Pembrokeshire"
        counties(47) = "Perth"
        counties(48) = "Peterborough"
        counties(49) = "Powys"
        counties(50) = "Rutland"
        counties(51) = "Scottish Borders"
        counties(52) = "Shropshire"
        counties(53) = "Somerset"
        counties(54) = "South Wales"
        counties(55) = "Staffordshire"
        counties(56) = "Stirling"
        counties(57) = "Suffolk"
        counties(58) = "Surrey"
        counties(59) = "Sussex"
        counties(60) = "Teeside"
        counties(61) = "Tyneside"
        counties(62) = "Ulster"
        counties(63) = "Warwickshire"
        counties(64) = "Wiltshire"
        counties(65) = "Worcestershire"
        counties(66) = "Wrexham"
        counties(67) = "Yorkshire"
        counties(68) = "Lancashire"
        For i = 0 To (counties.Length - 1)
            System.IO.Directory.CreateDirectory(txtroot.Text & "\" & counties(i))
            oWrite = oFile.CreateText(txtroot.Text & "\" & counties(i) & "\index.txt")
        Next i
        oWrite.Close()
        For i = 0 To (counties.Length - 1)
            oRead = oFile.OpenText(txtroot.Text & "\" & counties(i) & "\index.txt")
            oWrite.WriteLine(textforindex)
        Next i
    End Sub

Thanks in advance

Andrew
LVL 20
REA_ANDREWAsked:
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.

fahimnxbCommented:
Hi Andrew,

First of all

        For i = 0 To (counties.Length - 1)
            System.IO.Directory.CreateDirectory(txtroot.Text & "\" & counties(i))
            oWrite = oFile.CreateText(txtroot.Text & "\" & counties(i) & "\index.txt")
        Next i
        oWrite.Close()
        For i = 0 To (counties.Length - 1)
            oRead = oFile.OpenText(txtroot.Text & "\" & counties(i) & "\index.txt")
            oWrite.WriteLine(textforindex)
        Next i

Here you are closing oWrite and then trying to access it in second loop. This itself cause an exception message. Secondly you are closing that oWrite as its needed. Just make another instance of StreamWriter to write to index.txt or index.htm file.

Regards,
Me
REA_ANDREWAuthor Commented:
ok i created a new instance at the top, like this
Dim oWriteAgain As System.IO.StreamWriter
is that ok?

then i tried it and again i get this message

The process cannot access the file 'C:\Documents and Settings\Andy\Desktop\temp\Aberdeenshire\index.txt' because it is being used by another process.
fahimnxbCommented:
Andrew,

Use the following working code.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim counties(69) As String
        Dim i As Integer
        Dim oFile As System.IO.File
        Dim oWrite As System.IO.StreamWriter
        Dim textforindex As String
        Dim oRead As System.IO.StreamReader
        textforindex = "This is Test Text"
        counties(0) = "Aberdeenshire"
        counties(1) = "Anglesey"
        counties(2) = "Angus"
        counties(3) = "Argyle"
        counties(4) = "Avon"
        counties(5) = "Ayrshire"
        counties(6) = "Bedfordshire"
        counties(7) = "Berkshire"
        counties(8) = "Birmingham"
        counties(9) = "Buckinghamshire"
        counties(10) = "Cambridgeshire"
        counties(11) = "Carmarthenshire"
        counties(12) = "Ceredigion"
        counties(13) = "Cheshire"
        counties(14) = "Conwy"
        counties(15) = "Cornwall"
        counties(16) = "Cumbria"
        counties(17) = "Denbighshire"
        counties(18) = "Derbyshire"
        counties(19) = "Devon"
        counties(20) = "Dorset"
        counties(21) = "Durham"
        counties(22) = "Edinburgh"
        counties(23) = "Essex"
        counties(24) = "Fife"
        counties(25) = "Flintshire"
        counties(26) = "Gloucestershire"
        counties(27) = "Gwynedd"
        counties(28) = "Hampshire"
        counties(29) = "Herefordshire"
        counties(30) = "Hertfordshire"
        counties(31) = "Highland"
        counties(32) = "Kent"
        counties(33) = "Lanarkshire"
        counties(34) = "Leicestershire"
        counties(35) = "Linconshire"
        counties(36) = "London"
        counties(37) = "Manchester"
        counties(38) = "Merseyside"
        counties(39) = "Middlesex"
        counties(40) = "Moray"
        counties(41) = "Norfolk"
        counties(42) = "Northamptonshire"
        counties(43) = "Northumberland"
        counties(44) = "Nottinghamshire"
        counties(45) = "Oxfordshire"
        counties(46) = "Pembrokeshire"
        counties(47) = "Perth"
        counties(48) = "Peterborough"
        counties(49) = "Powys"
        counties(50) = "Rutland"
        counties(51) = "Scottish Borders"
        counties(52) = "Shropshire"
        counties(53) = "Somerset"
        counties(54) = "South Wales"
        counties(55) = "Staffordshire"
        counties(56) = "Stirling"
        counties(57) = "Suffolk"
        counties(58) = "Surrey"
        counties(59) = "Sussex"
        counties(60) = "Teeside"
        counties(61) = "Tyneside"
        counties(62) = "Ulster"
        counties(63) = "Warwickshire"
        counties(64) = "Wiltshire"
        counties(65) = "Worcestershire"
        counties(66) = "Wrexham"
        counties(67) = "Yorkshire"
        counties(68) = "Lancashire"
        For i = 0 To (counties.Length - 1)
            System.IO.Directory.CreateDirectory("C:\temp\" & counties(i))
            oWrite = oFile.CreateText("C:\temp\" & counties(i) & "\index.txt")
            oWrite.WriteLine(textforindex)
            oWrite.Close()
        Next i
        MsgBox("Directory Creation And Writing Done")
End Sub

Regards,
Me

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
fahimnxbCommented:
Replace c:\temp in loop with the same path(text box thing) you are using in your sample code. I forget to do it in hurry.

Regards,
Me
REA_ANDREWAuthor Commented:
worked a treat thank you very much.
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.NET

From novice to tech pro — start learning today.