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 = 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 reqFTP As FtpWebRequest = CType(Net.FtpWebRequest.Create(host), FtpWebRequest)
Dim ftpStream As Stream = Nothing
Dim Sr As StreamReader = Nothing
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
Catch ex As Exception
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