Results 1 to 6 of 6

Thread: The process cannot access the file because it is being used by another process

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    The process cannot access the file because it is being used by another process

    Hello guys,

    I have encounter this exception
    The process cannot access the file because it is being used by another process
    This is a windows service application.im using filesystemwatcher control to monitor a folder and then my VBA app. drop a CSV file to that folder.it works for the first time you drop a file but if i try to drop another file it will throw an exception.

    please any idea.what ive done wrong here.

    this is my code

    Code:
    private void folderWatcher_Created(object sender, FileSystemEventArgs e) 
            {
                try
                {
                    //Lookup all .csv in the given directory.
                    string[] localPath = Directory.GetFiles(@"C:\EG2003\COSBM\", "*.csv");
                    
                    
                    //Get the filename
                    string filename = localPath[0].Substring(localPath[0].Length - 7, 7);
    
                    //FilePath where we drop the file in HO.
                    String HOPath = string.Concat(@"C:\DTR_Fire\", filename);
    
                    using (testwebserver.COSBMService service = new testwebserver.COSBMService())
                    {
                        //service.WriteFile(HOPath, null, string.Empty);
    
                        using (FileStream fs = new FileStream(localPath[0], FileMode.Open, FileAccess.Read,FileShare.ReadWrite))
                        {
                            if (fs.Length > 0)
                            {
                                byte[] Data;
                                long ByteToRead;
    
                                while (fs.Position < fs.Length)
                                {
                                    if (fs.Length - fs.Position >= 102400)
                                    {
                                        ByteToRead = 102400;
                                    }
                                    else
                                    {
                                        ByteToRead = fs.Length - fs.Position;
                                    }
                         
                                    Data = new byte[ByteToRead];
                                    fs.Read(Data, 0, unchecked((int)ByteToRead));
                                    service.WriteFile(HOPath, Data, string.Empty);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogMessageToFile(ex.Message);
                }
                    
            }

  2. #2
    Addicted Member
    Join Date
    Mar 2007
    Location
    San Pedro de Macoris, Dominican Republic
    Posts
    211

    Re: The process cannot access the file because it is being used by another process

    Try closing the filestream.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2009
    Posts
    135

    Re: The process cannot access the file because it is being used by another process

    Quote Originally Posted by pukisoft View Post
    Try closing the filestream.
    i did but i get this error:
    Only assignment, call, increment, decrement, and new object expressions can be used as a statement

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: The process cannot access the file because it is being used by another process

    Here's the thing I've found about using the FSW looking for newly created files. The even fires off when a file is created... created, but not necessarily finished. huh? eh? wha? Consider this: any process that writes to a file first has to CREATE it before it can be written to. So that's what's happening... the file is being created, but still written to by the other process when you try to access it. this was a problem we ran into in a former life of mine... so what we did was each time the create event was fired, we'd get the file name, and put it into an array (at the time, we didn't have generics, otherwise we'd have used a Queue(Of T)) and start a timer. the timer had a 30-second delay. This usually gave the external process (we were receiving files via FTP) to complete the file transfer. If another file came in during that 30 seconds, the timer was stopped, reset, and restarted, allowing another 30 seconds. When the timer expired, we then took the array and loop through each file in it and process them.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: The process cannot access the file because it is being used by another process

    Yeah, ran into that problem in a previous job before.

    Check out this link: http://bloggingabout.net/blogs/jschr.../06/12886.aspx

    Here's the solution I came up with in the past: vb dot net forums.com/vb-net-general-discussion/40290-help-file-watcher-class-visual-basic-net.html#post116289 (remove the spaces)

    @Mods - Very classy auto renaming the links to the other forum to ***************s.com.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: The process cannot access the file because it is being used by another process

    Matt - the link gets renamed because of some previous bad behavior... it was blacklisted for some reason (I don't remember off hand what the problem was... but it is what it is).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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