Microsoft Visual Web Developer 2008 Express Edition. Behind code is VB.net
I am using DownLoadFile to copy a file from my website to my local PC. I have enclosed the process in try/catch/end try. I receive no errors. Upon completion of the process I check for the existence of the file at copyto from within the code. The result is that it does exist. As a further test, I open the file, read to completion and then place one of the lines from the read file in a label on the webform. Everything appears positive except the file does not really exist at the copyto location. I have displayed the value in the copyto variable and it is where I expected it to be.
copyfrom is set as copyfrom = Server.MapPath("/Set Directory/")
copyto is set as copyto = "c:\Set Directory\"
filename is selected from a DropDownBox that contains the names of the files on the website (copyfrom).
The relevant code is:
Sub downloadit()
Dim ps As New PermissionSet(PermissionState.None)
Dim fileReader As New WebClient()
fileReader.Credentials = CredentialCache.DefaultNetworkCredentials
fileReader.Headers.Add(HttpRequestHeader.UserAgent, "anything")
ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, (copyfrom) & (filename)))
ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, (copyto) & (filename)))
ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.AllAccess, (copyfrom) & (filename)))
ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.AllAccess, (copyto) & (filename)))
filename = (filename) & ".TXT"
Try
fileReader.DownloadFile((copyfrom) & (filename), (copyto) & (filename))
If System.IO.File.Exists((copyto) & (filename)) Then
Label1.Text = "Successfully Copied " + copyfrom + (filename) + " " & (copyto) & (filename)
readit()
End If
Catch ex As Exception
Label1.Text = "Error copying the file" + copyfrom + (filename) + " " & (copyto) & (filename) & " - " + ex.Message
End Try
end sub
Sub readit()
ln = 0
FileOpen(1, ((copyto) & (filename)), OpenMode.Input)
Do Until EOF(1)
ln = ((ln) + 1)
testline(ln) = LineInput(1).Trim
Loop
FileClose(1)
FileOpen(1, ((copyto) & "2" & (filename)), OpenMode.Output)
For a = 1 To (ln)
PrintLine(1, (testline(a)))
Label1.Text = (testline(2))
Next
FileClose(1)
End Sub
I am able to copy the file from one folder to another on the website with no problems.
I can't figure out why the code is able to read the file at the copyto location even though it is not actually there. I am baffled!
Any suggestions would be welcome.
I am using DownLoadFile to copy a file from my website to my local PC. I have enclosed the process in try/catch/end try. I receive no errors. Upon completion of the process I check for the existence of the file at copyto from within the code. The result is that it does exist. As a further test, I open the file, read to completion and then place one of the lines from the read file in a label on the webform. Everything appears positive except the file does not really exist at the copyto location. I have displayed the value in the copyto variable and it is where I expected it to be.
copyfrom is set as copyfrom = Server.MapPath("/Set Directory/")
copyto is set as copyto = "c:\Set Directory\"
filename is selected from a DropDownBox that contains the names of the files on the website (copyfrom).
The relevant code is:
Sub downloadit()
Dim ps As New PermissionSet(PermissionState.None)
Dim fileReader As New WebClient()
fileReader.Credentials = CredentialCache.DefaultNetworkCredentials
fileReader.Headers.Add(HttpRequestHeader.UserAgent, "anything")
ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, (copyfrom) & (filename)))
ps.AddPermission(New EnvironmentPermission(EnvironmentPermissionAccess.AllAccess, (copyto) & (filename)))
ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.AllAccess, (copyfrom) & (filename)))
ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.AllAccess, (copyto) & (filename)))
filename = (filename) & ".TXT"
Try
fileReader.DownloadFile((copyfrom) & (filename), (copyto) & (filename))
If System.IO.File.Exists((copyto) & (filename)) Then
Label1.Text = "Successfully Copied " + copyfrom + (filename) + " " & (copyto) & (filename)
readit()
End If
Catch ex As Exception
Label1.Text = "Error copying the file" + copyfrom + (filename) + " " & (copyto) & (filename) & " - " + ex.Message
End Try
end sub
Sub readit()
ln = 0
FileOpen(1, ((copyto) & (filename)), OpenMode.Input)
Do Until EOF(1)
ln = ((ln) + 1)
testline(ln) = LineInput(1).Trim
Loop
FileClose(1)
FileOpen(1, ((copyto) & "2" & (filename)), OpenMode.Output)
For a = 1 To (ln)
PrintLine(1, (testline(a)))
Label1.Text = (testline(2))
Next
FileClose(1)
End Sub
I am able to copy the file from one folder to another on the website with no problems.
I can't figure out why the code is able to read the file at the copyto location even though it is not actually there. I am baffled!
Any suggestions would be welcome.