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);
            }
                
        }