Hi
I currently am running a process where files are fetch from a sever and imported into the database.
So when a user clicks the button the following code is executed
Dim host As String = "ftp://*/**/**/***/****"
Dim username As String = "*********"
Dim password As String = "*******"
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 sr As StreamReader = Nothing
Dim reqFTP As FtpWebRequest = CType(Net.FtpWebRequest.Create(host), FtpWebRequest)
Dim ftpStream As Stream = Nothing
Try
sr = New StreamReader(fwr.GetResponse().GetResponseStream())
Do Until (sr.EndOfStream())
filename = sr.ReadLine()
Dim filepath As String = (Me.Page.Server.MapPath("..\MyFolder\" + filename + ".txt"))
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
End Try
If (Not sr Is Nothing) Then sr.Close() : sr = Nothing
If (Not fwr Is Nothing) Then fwr = Nothing
Dim strFileSize As String = ""
Dim di As New DirectoryInfo(Me.Page.Server.MapPath("..\MyserverFolder"))
Dim aryFi As FileInfo() = di.GetFiles("*.txt")
Dim fi As FileInfo
Dim sConnStr As String = ConfigurationManager.AppSettings("myConnectionString")
Dim cn As New System.Data.SqlClient.SqlConnection(sConnStr)
Dim cmdInsertInto As New System.Data.SqlClient.SqlCommand("", cn)
cmdInsertInto.CommandType = System.Data.CommandType.Text
cmdInsertInto.CommandTimeout = 0
Dim count As Integer = 0
For Each fi In aryFi
cn.Open()
Dim file As New FileStream(fi.FullName, FileMode.Open, FileAccess.Read)
Dim stream As New StreamReader(file)
I am trying to automate this code to be executed every 24 hours
so i have added a timmer in the Global.asax page
this is my code for the timmer
Dim timer As New System.Timers.Timer()
timer.Enabled = True
timer.Start()
Dim interval As Double = 86400000
timer.Interval = interval
AddHandler timer.Elapsed, AddressOf OnElapsedTime
and in the sub OnElapsedTime i have the code above for the import that i use on the button
the issue i am getting is that in the global.asax i get an error on this line
Dim filepath As String = (Me.Page.Server.MapPath("..\MyFolder\" + filename + ".txt"))
i have tried Dim filepath As String = (Me.Server.MapPath("..\MyFolder\" + filename + ".txt"))
as well as Dim filepath As String = (Server.MapPath("..\MyFolder\" + filename + ".txt"))
the error is "Server operation is not available in this context."
Please could someone assist