其他进程正在使用该文件

其他进程正在使用该文件

本文介绍了继续收到以下错误进程无法访问该文件,因为当将文件从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

推荐答案


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

07-30 16:26