ftp code



Download file

           WebClient client = new WebClient();
            client.Credentials = new NetworkCredential("ftp", "qet");
            client.DownloadFile(
                "ftp://192.168.16.15/ftp01/from/test.txt", @"d:\test.txt");



from https://stackoverflow.com/questions/2781654/ftpwebrequest-download-file


The most trivial way to download a binary file from an FTP server using .NET framework is using WebClient.DownloadFile:
WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.DownloadFile(
    "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip");

Advanced options

Use FtpWebRequest, only if you need a greater control, that WebClient does not offer (like TLS/SSL encryption, progress monitoring etc). Easy way is to just copy an FTP response stream to FileStream using Stream.CopyTo:
FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
    ftpStream.CopyTo(fileStream);
}

Progress monitoring

If you need to monitor a download progress, you have to copy the contents by chunks yourself:
FtpWebRequest request =
    (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip");
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.DownloadFile;

using (Stream ftpStream = request.GetResponse().GetResponseStream())
using (Stream fileStream = File.Create(@"C:\local\path\file.zip"))
{
    byte[] buffer = new byte[10240];
    int read;
    while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
    {
        fileStream.Write(buffer, 0, read);
        Console.WriteLine("Downloaded {0} bytes", fileStream.Position);
    }
}








using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

class FtpUtil
{

    public static string ID { get; set; }
    public static string PW { get; set; }


    public static void SetupSFTP(string id, string pw)
    {
        ID = id;
        PW = pw;
    }

    public static void SetupFTP(string id, string pw)
    {
        ID = id;
        PW = pw;
    }


    public static void Download(string uri, string localPath)
    {
        WebClient client = new WebClient();
        client.Credentials = new NetworkCredential(ID.Normalize(), PW.Normalize());
        client.DownloadFile(uri, localPath); 
    }


    public static List<string> List(string uri)
    {
        List<string> directories = new List<string>();

        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
        ftpRequest.Credentials = new NetworkCredential(ID.Normalize(), PW.Normalize());
        ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
        //FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
        FtpWebResponse response = null;
        ftpRequest.KeepAlive = false;
        using (response = (FtpWebResponse)ftpRequest.GetResponse())
        {

            StreamReader streamReader=null;
            using (streamReader = new StreamReader(response.GetResponseStream()))
            {
                string line = streamReader.ReadLine();
                while (!string.IsNullOrEmpty(line))
                {
                    directories.Add(line);
                    line = streamReader.ReadLine();
                }
            }


        }





        return directories;
    }

    public static List<string> ReadText(string uri)
    {
        List<string> lines = new List<string>();
        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(uri);
        ftpRequest.Credentials = new NetworkCredential(ID.Normalize(), PW.Normalize());
        ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
        ftpRequest.KeepAlive = false;

        try
        {
            using (Stream stream = ftpRequest.GetResponse().GetResponseStream())
            //using (StreamReader reader = new StreamReader(stream, Encoding.ASCII))
            using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("big5")))
            {
                while (!reader.EndOfStream)
                {
                    lines.Add(reader.ReadLine());
                }
            }
        }
        catch (Exception ex)
        {
            FileUtil.Write(System.Reflection.MethodInfo.GetCurrentMethod().ToString() + "," + ex.Message.ToString());
            //throw;
        }
        return lines;

    }

    public static void Move(string fromUri, string toUri)
    {
        try
        {
            Uri uri = new Uri(fromUri);
            string destPath = toUri;
            FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(uri);
            ftpReq.Credentials = new NetworkCredential(ID.Normalize(), PW.Normalize());
            ftpReq.Method = WebRequestMethods.Ftp.Rename;
            ftpReq.KeepAlive = false;
            ftpReq.RenameTo = destPath;
            FtpWebResponse ftpRes = (FtpWebResponse)ftpReq.GetResponse();
        }
        catch (Exception ex)
        {           
            throw;
        }       
    }

    public static void Upload(string ftpUriTo, string localFile)
    {
        using (var client = new WebClient())
        {
            client.Credentials = new NetworkCredential(ID.Normalize(), PW.Normalize());
            client.UploadFile(ftpUriTo, WebRequestMethods.Ftp.UploadFile, localFile);
        }
    }




}










留言

熱門文章