|
-
Jul 24th, 2008, 05:55 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] [2008] FTP in Binary -Please Help
Good Morning,
I am trying to make a Windows Service in VB.Net which I can do perfectly fine. Its job is to open an ftp connection every night and download 3 files from a Unix server plus a log file. Now I have got it working perfectly apart from it doesn't download in Binary but instead ASCII which is a bit of a problem as it corrupts the files. The files are sensative compressed backup files from a Unix box that need to be able to be put back across at any point and still work. I was wondering if anyone can help me with Binary FTP in vb.net. Here is the method I currently use:
Code:
My.Computer.Network.DownloadFile("ftp://servername/folder/file.Z", "C:\backup\file.Z", "username", "password")
Which is nice and quick easy to use, ok it doesn't boast any features like telling you how much it's dont but I don't need that its running in the background as a service at 3 in the morning noone is watching it then as its goes through its tasks it just writes to a log file of how its doing and any errors its come accross so the next morning I can see if anything went wrong.
-
Jul 24th, 2008, 05:58 AM
#2
Re: [2008] FTP in Binary -Please Help
If you need more control then use an FtpWebRequest.
-
Jul 24th, 2008, 08:18 AM
#3
Thread Starter
Frenzied Member
Re: [2008] FTP in Binary -Please Help
Hi, thanks for the reply I have been looking into it and I have managed to get something working but not exactly how I want I have been fiddling for a couple of hours but I am not understanding that well. I can get it to list directory or use the download file command but it won't do what I want.
Basically If I use the downloadfile command by editing the example I get it opens the file up and displays its contents in a rich text box instead of creating a file. (which will bring me to a second point once I have finished this)
Code:
This is the code I have got so far:
Dim getfile As FtpWebRequest = FtpWebRequest.Create(tboServer.Text)
getfile.UseBinary = True
getfile.Credentials = New NetworkCredential(tboUser.Text, tboPass.Text)
getfile.Method = WebRequestMethods.Ftp.DownloadFile
Dim sr As New StreamReader(getfile.GetResponse().GetResponseStream())
Dim str As String = sr.ReadLine()
While Not str Is Nothing
outputtxt.Text = str & Environment.NewLine & outputtxt.Text
str = sr.ReadLine()
End While
sr.Close()
sr = Nothing
fwr = Nothing
Also my second point is if the file is a big file that method crashes near the end saying that the stream was closed before it was meant to but thats the only code in the whole program there is nothing interfering if I do the same code on a 4 line text file it works perfectly but give it a few hundred lines and it don't work.
Your advice is appreciated
Thanks,
Max
-
Jul 24th, 2008, 10:15 AM
#4
Thread Starter
Frenzied Member
Re: [2008] FTP in Binary -Please Help
Hi, Just to let you know I have managed to resolve the problem and have pasted my code below to anyone else who needs a helping hand:
Code:
Dim getfile As FtpWebRequest = FtpWebRequest.Create("ftp://serverordomainname/file.ext")
getfile.UseBinary = True
getfile.Credentials = New NetworkCredential("username", "password")
getfile.Method = WebRequestMethods.Ftp.DownloadFile
Using response As System.Net.FtpWebResponse = _
CType(getfile.GetResponse, System.Net.FtpWebResponse)
Using responseStream As IO.Stream = response.GetResponseStream
'loop to read & write to file
Using fs As New IO.FileStream("C:\filename.ext", IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0 'see Note(1)
responseStream.Close()
fs.Flush()
fs.Close()
End Using
responseStream.Close()
End Using
response.Close()
End Using
That downloads straight to a file in Binary mode for unix files. :-)
Thanks for the help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|