继续收到以下错误进程无法访问该文件,因为当将文件从FTP复制到本地服务器时,其他进程正在使用该文件 [英] Keep getting the following error The process cannot access the file because it is being used by another process when copying a file from FTP to my local server

查看:235
本文介绍了继续收到以下错误进程无法访问该文件,因为当将文件从FTP复制到本地服务器时,其他进程正在使用该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来将文件从FTP服务器下载到我的本地主机的代码,但我一直收到错误该进程无法访问该文件,因为它正被另一个进程使用。



请您查看我的代码并告诉我可能出错的地方



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

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)在这一行我收到错误




Dim file As New FileStream(Fi.FullName, FileMode.Open, FileAccess.Read) at this line i am getting the error

推荐答案

这些文件的平均大小是什么?



如果你下载一个0 kb的文件,会发生这种情况吗?



是什么引起了确切的错误?



ps - Doh!这是没有解决方案,我点击了错误的按钮!对不起。



PS2 - 我认为当你试图访问它时文件仍在写入。我不久前遇到了这个问题并用计时器解决了。
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.


这篇关于继续收到以下错误进程无法访问该文件,因为当将文件从FTP复制到本地服务器时,其他进程正在使用该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆