Link to home
Start Free TrialLog in
Avatar of REA_ANDREW
REA_ANDREWFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of fahimnxb
fahimnxb

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
Avatar of REA_ANDREW

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of fahimnxb
fahimnxb

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
worked a treat thank you very much.