Need some C# help v.Ftpwebrequest

Discussion in 'OT Technology' started by JoeyJoJoJuniorShabadoo, Sep 29, 2006.

  1. JoeyJoJoJuniorShabadoo

    JoeyJoJoJuniorShabadoo Live Free or Die

    Joined:
    Nov 9, 2004
    Messages:
    4,347
    Likes Received:
    0
    Location:
    http://www.stogietime.com
    So I just started a new job programming in c#, which I have not used c# before. I am making a live update area of the main program so I have to download files from a FTP server.

    I am trying to use Ftpwebrequest to do this but I cannot figure out how to download files to the disk other than a text file. All the examples online show how you can log in to the ftp server and then get the text file and either print out to the screen or save to a file. Thats nice and all, but when I have other files that are not text files, such as jpg's or zip files, it does not work.

    Does anyone know of any good examples or somewhere that could lead me in the right direction to figure this out? I have been searching google for a couple days now and I can't seem to figure it out.

    :x:
     
  2. SLED

    SLED build an idiot proof device and someone else will

    Joined:
    Sep 20, 2001
    Messages:
    28,118
    Likes Received:
    0
    Location:
    AZ, like a bauce!
    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create( "ftp://blah.com/file.bmp" );
    string localFile = "C:\\FeatherTexture.bmp";

    ftp.Credentials = new NetworkCredential( "xxx", "xxx" );

    ftp.KeepAlive = false;
    ftp.UseBinary = true;
    ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;

    FtpWebResponse response;
    System.IO.Stream stream;
    System.IO.FileStream fstream;

    try {
    response = (FtpWebResponse)ftp.GetResponse();
    stream = response.GetResponseStream();
    fstream = new System.IO.FileStream( localFile, System.IO.FileMode.Create );

    byte[] buff = new byte[2047];
    int read = 0;

    do {
    read = stream.Read(buff, 0, buff.Length );
    fstream.Write( buff, 0, read );

    } while( read != 0 );
    response.Close();
    fstream.Flush();
    fstream.Close();
    response.Close();
    } catch( Exception ex ) {
    MessageBox.Show( ex.Message );
    } finally {

    }
     
  3. JoeyJoJoJuniorShabadoo

    JoeyJoJoJuniorShabadoo Live Free or Die

    Joined:
    Nov 9, 2004
    Messages:
    4,347
    Likes Received:
    0
    Location:
    http://www.stogietime.com
    thanks a ton for your reply. I am going to give this a try on monday morning.
     

Share This Page