Results 1 to 7 of 7

Thread: [2005] The process cannot access the file...WebClient.Downloadfile()

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    [2005] The process cannot access the file...WebClient.Downloadfile()

    Hey Guys,

    I am attempting to open a CSVfile with a StreamReader that I have downloaded using WebClient.DownloadFile() but am receiving error:

    The process cannot access the file 'x.csv' because it is being used by another process.
    Code:
            Dim wb As New System.Net.WebClient()
           
            wb.DownloadFile(sSource, sDataFilePath)
    
            wb.Dispose()
            wb = Nothing
            
            Dim textReader As System.IO.StreamReader = Nothing
    
            textReader = New System.IO.StreamReader(sDataFilePath)
    The last line throws the exception. I think somehow the webclient is holding a lock on the file but how? I am disposing of the webclient and being extra cautious by setting it to Nothing.

    How else can I be sure the webclient has let go? Or better yet does anyone know exactly what is going on?

    Thanks ppl!
    Matt.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] The process cannot access the file...WebClient.Downloadfile()

    Not that it should make a difference but I would first suggest that you do this:
    vb.net Code:
    1. Using wb As New WebClient
    2.     'Use wb here.
    3. End Using
    You should always employ a Using block for short-lived, disposable objects. The object will be disposed automatically when execution leaves the block no matter what happens within, including Return statements or exceptions being thrown.

    Secondly, unless you really want a local file I would suggest using the DowloadString method to get the data directly into a String rather than using the local file system.

    If you do want a local file then I'd guess that you're just a bit too fast in trying to access it. Try this to ensure that the file is available before opening it:
    vb.net Code:
    1. While (IO.File.GetAttributes(sDataFilePath) And IO.FileAttributes.Offline) <> IO.FileAttributes.Offline
    2.     'Wait 100 milliseconds before trying to access the file.
    3.     Threading.Thread.Sleep(100)
    4. End While
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] The process cannot access the file...WebClient.Downloadfile()

    Hey JM,

    Great suggestions!

    Sadly the using block made no difference as expected by yourself.

    I slept the thread for 10 seconds before I tried to open the file and still no luck, didn't think of your loop though - nice code. I didn't implement that exact method, I decided that if the file hadn't responded in 10K milliseconds then the problem must lie deeper.

    Interestingly; I runs PERFECTLY from code. Compiled EXE = different story.

    I hadn't thought of dumping it into memory via DownloadString(), reason being: the file could get quite large (>1Mb) so I hadn't considered it.

    Question: I am reading the file one line at a time, I'm assuming that the framework doesn't buffer the entire stream into memory. But does it? If so then I have no argument for placing the file on the local disk, sequentially reading and deleting it after operations. I may as well download it directly to memory (DownloadString()) and operate on it from there - would be quicker and not dependent on the file system.

    Thoughts?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] The process cannot access the file...WebClient.Downloadfile()

    If you don't actually need a local file then it would be preferable to do without one. I guess you have to use your judgement to determine whether you think the source is too big to download to memory rather than a local file. 1 MB wouldn't be a big deal under most conditions, but there would obviously be a limit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] The process cannot access the file...WebClient.Downloadfile()

    Hey JM,

    Just an update, used DownloadString() with great success. Its quicker and seems more reliable. Most of my data files are under 500K, the odd one spiking to 1Mb so memory allocation wont be a concern.

    Lovin it mate! Thanks for the wisdom....

    Matt.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] The process cannot access the file...WebClient.Downloadfile()

    It may not be of advantage to you but you should also note that there is a StringReader class that allows you to read a String the same way a StreamReader allows you to read a file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    65

    Re: [2005] The process cannot access the file...WebClient.Downloadfile()

    Hehehe Spot on! Already using it mate - again good suggestion. Using it to read each line of the downloaded data....

    I would still love to know whats going on with the WebClient, if I had time I would do some tests, I wanna call GC.Collect after I dispose of the webclient object and see if it makes a difference. Time....

    Thanks JM...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width