Click here to Skip to main content
15,891,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code i am using to download a file from FTP server to my local host but i keep getting the error The process cannot access the file because it is being used by another process.

Please could you view my code and tell me where i may be going wrong

VB
Public Sub Timer()
       Dim timmer As New System.Timers.Timer()
       timmer.Enabled = True
       timmer.Start()
       ''Dim Interval As Double = 3600000
       Dim Interval As Double = 300000
       timmer.Interval = Interval
       AddHandler timmer.Elapsed, AddressOf OnElapsedTime
   End Sub


   Public Sub OnElapsedTime(Source As Object, e As ElapsedEventArgs)
       Dim host As String = "ftp://ftpadress"
       Dim username As String = "usrname"
       Dim password As String = "Password"
       Dim Path As String = "\Import\"


       Dim Fwr As Net.FtpWebRequest = CType(Net.FtpWebRequest.Create(host), FtpWebRequest)
       Fwr.Credentials = New NetworkCredential(username, password)
       Fwr.KeepAlive = True
       Fwr.Method = WebRequestMethods.Ftp.ListDirectory
       Dim Filename As String
       ' Dim mydocpath As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
       Dim reqFTP As FtpWebRequest = CType(Net.FtpWebRequest.Create(host), FtpWebRequest)
       Dim ftpStream As Stream = Nothing
       Dim Sr As StreamReader = Nothing
       ' Dim fp As String = AppDomain.CurrentDomain.BaseDirectory + "Import\" + Filename + ".csv"

       Try
           Sr = New StreamReader(Fwr.GetResponse().GetResponseStream())
           Do Until (Sr.EndOfStream())
               Filename = Sr.ReadLine()
               Dim filepath As String = (AppDomain.CurrentDomain.BaseDirectory + "Import\" + Filename)
               Dim outputStream As New FileStream(filepath, FileMode.Create)

               reqFTP = DirectCast(FtpWebRequest.Create(New Uri(host + "/" + Filename)), FtpWebRequest)
               reqFTP.Credentials = New NetworkCredential(username, password)
               reqFTP.KeepAlive = True
               reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
               reqFTP.UseBinary = True

               Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
               ftpStream = response.GetResponseStream()
               Dim cl As Long = response.ContentLength
               Dim buffersize As Integer = 2048
               Dim readcount As Integer
               Dim buffer As Byte() = New Byte(buffersize - 1) {}


               readcount = ftpStream.Read(buffer, 0, buffersize)
               While readcount > 0
                   OutputStream.Write(buffer, 0, readcount)
                   readcount = ftpStream.Read(buffer, 0, buffersize)


               End While

            ftpStream.Close()
               outputStream.Close()
               response.Close()


               Dim FTPRequest As FtpWebRequest = CType(Net.WebRequest.Create(host), FtpWebRequest)
               FTPRequest = DirectCast(FtpWebRequest.Create(New Uri(host + "/" + Filename)), FtpWebRequest)
               FTPRequest.Credentials = New System.Net.NetworkCredential(username, password)
               FTPRequest.KeepAlive = True
               FTPRequest.Method = WebRequestMethods.Ftp.DeleteFile
               Dim ftpResponse As FtpWebResponse = DirectCast(FTPRequest.GetResponse(), FtpWebResponse)
           Loop
           ' Fwr.KeepAlive = False
       Catch ex As Exception
           ' Me.Page.ErrorOnPage = True

           ' Report the error message to the end user
           ' Utils.MiscUtils.RegisterJScriptAlert(Me, "BUTTON_CLICK_MESSAGE", ex.Message)

       Finally
         
       If (Not Sr Is Nothing) Then Sr.Close() : Sr = Nothing
       If (Not Fwr Is Nothing) Then Fwr = Nothing
       End Try

      

       Dim di As New DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "Import\")
       Dim aryFi As FileInfo() = di.GetFiles
       Dim Fi As System.IO.FileInfo

       Dim StrCon As String = ConfigurationManager.AppSettings("DbConString")
       Dim con As New SqlConnection(StrCon)
       Dim Cmd As New SqlCommand
       Dim reader As SqlDataReader

       For Each Fi In aryFi


           Dim file As New FileStream(Fi.FullName, FileMode.Open, FileAccess.Read)



Dim file As New FileStream(Fi.FullName, FileMode.Open, FileAccess.Read) at this line i am getting the error
Posted
Comments
Prasad Avunoori 13-May-14 4:57am    
Make sure that file is not opened by any other user.

1 solution

Whats tha average size of these files ?

Does this happens if you download a 0 kb file ?

Whats the exact error being thrown ?

ps - Doh ! this is no solution, I clicked the wrong button ! Sorry for that.

PS2 - Im thinking that the file is still being writen when you try to access it. I had this problem a while ago and solved it with a timer.
 
Share this answer
 
v3
Comments
isi19 13-May-14 5:44am    
The Files are not very big between 1-10kb.

The exact error i am getting is
The process cannot access the file because it is being used by another process.

any idea how i could resolve this issue?
_Vitor Garcia_ 13-May-14 9:35am    
Check which application is using your files with http://lockhunter.com/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900