天天看點

C# 從FTP伺服器下載下傳檔案到本地

static void Main(string[] args)
        {
            //ftp伺服器路徑
            string ftpServer = System.Configuration.ConfigurationSettings.AppSettings["FtpServer"].ToString();
            //ftp本地路徑
            string ftpDefaultUrl = System.Configuration.ConfigurationSettings.AppSettings["FtpDefaultUrl"].ToString();
            //登入到ftp的賬号
            string ftpUserName = System.Configuration.ConfigurationSettings.AppSettings["LoginID"].ToString();
            //登入到ftp的密碼
            string ftpUserPwd = System.Configuration.ConfigurationSettings.AppSettings["LoginPWD"].ToString();
            //下載下傳後的檔案存放路徑
            string downloadUrl = System.Configuration.ConfigurationSettings.AppSettings["DownloadPath"].ToString();
            //需要下載下傳的檔案名
            string fileName =  "test.txt";
            //需要現在的檔案在ftp上的完整路徑
            string fileUploadPath = ftpServer + ftpDefaultUrl;
            Uri uri = new Uri(fileUploadPath+"/"+fileName);
            //下載下傳後存放的路徑
            string FileName = Path.GetFullPath(downloadUrl) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath); 
            
            //建立檔案流
            FileStream fs = null;
            Stream responseStream = null;
            try {
                //建立一個與FTP伺服器聯系的FtpWebRequest對象
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
                //設定請求的方法是FTP檔案下載下傳
                request.Method = WebRequestMethods.Ftp.DownloadFile;
                //連接配接登入FTP伺服器
                request.Credentials = new NetworkCredential(ftpUserName, ftpUserPwd);
                //擷取一個請求響應對象
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                //擷取請求的響應流
                responseStream = response.GetResponseStream();
                //判斷本地檔案是否存在,如果存在,則打開和重寫本地檔案
                if (File.Exists(FileName))
                {
                    fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);
                }
                else
                {
                    fs = File.Create(FileName);
                }
            
               if (fs != null)
                {
                    int buffer_count = 65536;
                    byte[] buffer = new byte[buffer_count];
                    int size = 0;
                    while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0)
                    {
                        fs.Write(buffer, 0, size);
                    }
                    fs.Flush();
                    fs.Close();
                    responseStream.Close();
                }
            }
            finally
            {
                if (fs != null)
                    fs.Close();
                if (responseStream != null)
                    responseStream.Close();
            }
        }
           

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="FtpServer" value="ftp://192.168.123.156/"/>
    <add key="FtpDefaultUrl" value="/Upload"/>
    <add key="LoginID" value="zs"/>
    <add key="LoginPWD" value="[email protected]"/>
    <add key="DownloadPath" value="/Download"/>
  </appSettings>
</configuration>  
           
C# 從FTP伺服器下載下傳檔案到本地

繼續閱讀