locked
File still in use after close() and dispose() RRS feed

  • Question

  • User661479005 posted

    I have created a CMS-system and I am using it on different homepages.
    To manage updates quick and easy I made a administration-system for uploading the updates on all homepages on the same time.
    As long as I only have to overwrite the old files everything is going fine, but there are some few files on every homepage containing some different constants.
    For that porpose I made this code:

    Open the new file to be copied from my default cms-system:

       Dim fs as new FileStream("D:\WEB\LOCALUSER\mysite.dk\skabelon\default.aspx",FileMode.OpenOrCreate)
       Dim objReader as new StreamReader(fs)
       Dim strFile as string = objReader.ReadToEnd
       objReader.close()
       fs.dispose()
       Dim strNewFile as string = ""
    For every url that have to be updated change the file:
       Dim strDefaultText as string = "<title>"My default title"</title>"
       Dim strDefaultNew as string = "<title>" & varNewTitle & "</title>"
    strNewFile = replace(strFile,strDefaultText,strDefaultNew)
    I save the file in a temporary folder:
    Dim objWriter as new streamwriter("D:\WEB\LOCALUSER\mysite.dk\skabelon\temp\" & tbFilnavn.Text,false)
    	 objWriter.Writeline(strNewFile)
    	 objWriter.close
    	 objWriter.dispose()
    	 objWriter = nothing
     
    Now I want to transfer the file to the new destination:
     
     Dim newFileSource as string = "D:\WEB\LOCALUSER\mysite.dk\skabelon\temp"
      
    
            myFtpWebRequest = WebRequest.Create("ftp://ftp." & it.Text & "/public_html/" & tbFilnavn.Text)
            myFtpWebRequest.Credentials = New NetworkCredential(ftpBrugernavn, ftpPass)
    
            myFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile
            myFtpWebRequest.UseBinary = True
    
            myStreamWriter = New StreamWriter(myFtpWebRequest.GetRequestStream())
            myStreamWriter.Write(New StreamReader(newFileSource & "\" & tbFilnavn.Text).ReadToEnd)
           
           myFtpWebResponse.Close()
            myStreamWriter.Close()
           myStreamWriter.dispose()
    	 
    
            myFtpWebResponse = myFtpWebRequest.GetResponse()
     
    Sometimes first file is updated succesfully, but for the next file I got this error:
    'D:\WEB\LOCALUSER\mysite.dk\skabelon\temp\default.aspx' because it is being used by another process.
    I tried to close and dispose everything possible, but still the file is in use for minutes.
    Tuesday, May 12, 2009 11:09 AM

Answers

  • User661479005 posted

    Hi - finally I found the bug in this line: 

    myStreamWriter.Write(New StreamReader(newFileSource & "\" & tbFilnavn.Text).ReadToEnd)

    The streamReader is not getting closed

    I changed the code like this:

       Dim nfs as new FileStream(newFileSource & "\" & tbFilnavn.Text,FileMode.OpenOrCreate)
       Dim nobjReader as new StreamReader(nfs)
       Dim nstrFile as string = nobjReader.ReadToEnd
      
            myStreamWriter = New StreamWriter(myFtpWebRequest.GetRequestStream())
            myStreamWriter.Write(nstrFile)
       nobjReader.close()
       nfs.dispose()

    Now it works

    Thank you anyway!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2009 4:34 AM

All replies

  • User1282917655 posted

    did you have an antiviruse in your pc ?

    Tuesday, May 12, 2009 11:31 AM
  • User661479005 posted

    Hi, Yes I have AVG professional in my pc, but my homepages are on hosted server.

    Wednesday, May 13, 2009 2:18 AM
  • User661479005 posted

    Hi - finally I found the bug in this line: 

    myStreamWriter.Write(New StreamReader(newFileSource & "\" & tbFilnavn.Text).ReadToEnd)

    The streamReader is not getting closed

    I changed the code like this:

       Dim nfs as new FileStream(newFileSource & "\" & tbFilnavn.Text,FileMode.OpenOrCreate)
       Dim nobjReader as new StreamReader(nfs)
       Dim nstrFile as string = nobjReader.ReadToEnd
      
            myStreamWriter = New StreamWriter(myFtpWebRequest.GetRequestStream())
            myStreamWriter.Write(nstrFile)
       nobjReader.close()
       nfs.dispose()

    Now it works

    Thank you anyway!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 13, 2009 4:34 AM