C# Ftp Client 基本操作


C# Ftp Client 上傳、下載與刪除

 

    簡單介紹一下Ftp Client 上傳、下載與刪除,這是目前比較常用的命令,各個方法其實都差不多,重點是了解Ftp命令協議。

1.建立連接

        public static string Connect(string path, string Login, string Password)
        {
            try
            {
                // 根據uri創建FtpWebRequest對象
                reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(path));

                //指定命令
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;

                // 指定數據傳輸類型
                reqFTP.UseBinary = true;

                // ftp用戶名和密碼
                reqFTP.Credentials = new NetworkCredential(Login, Password);

                //
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

                return "FTP連接成功";
            }
            catch(Exception ex)
            {
                return "FTP連接失敗," + ex.Message;
            }
            
        }

2.上傳文件

        public static string UploadFile(string filename, string FtpPath, string Login, string Password)
        {
            try

            {
                FileInfo fileInf = new FileInfo(filename);

                //判斷是否有上級目錄

                string uri = "ftp://" + Path.Combine(FtpPath, fileInf.Name);

                // 根據uri創建FtpWebRequest對象
                reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri));

                // 指定數據傳輸類型
                reqFTP.UseBinary = true;

                // ftp用戶名和密碼
                reqFTP.Credentials = new NetworkCredential(Login, Password);

                // 默認為true,連接不會被關閉

                // 在一個命令之后被執行

                reqFTP.KeepAlive = false;

                // 指定執行什么命令

                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

                // 上傳文件時通知服務器文件的大小

                reqFTP.ContentLength = fileInf.Length;

                // 緩沖大小設置為kb 

                int buffLength = 2048;

                byte[] buff = new byte[buffLength];

                int contentLen;

                // 打開一個文件流(System.IO.FileStream) 去讀上傳的文件

                FileStream fs = fileInf.OpenRead();



                // 把上傳的文件寫入流

                Stream strm = reqFTP.GetRequestStream();

                // 每次讀文件流的kb 

                contentLen = fs.Read(buff, 0, buffLength);

                // 流內容沒有結束

                while (contentLen != 0)

                {

                    // 把內容從file stream 寫入upload stream 

                    strm.Write(buff, 0, contentLen);

                    contentLen = fs.Read(buff, 0, buffLength);

                }

                // 關閉兩個流

                strm.Close();

                fs.Close();
                //Successinfo
                return string.Format("本地文件{0}已成功上傳", fileInf.Name);
            }

            catch (Exception ex)

            {
                //ErrorInfo
                return "上傳失敗" + ex.Message;
            }

        }

3.下載文件

        public static string DownloadFile(string fileDownPath, string fileName, string FtpPath, string Login, string Password)
        {
            try
            {
                string onlyFileName = Path.GetFileName(fileName);

                string newFileName = fileDownPath + onlyFileName;

                if (File.Exists(newFileName))

                {

                    string errorinfo = string.Format("文件{0}在該目錄下已存在,無法下載", fileName);

                    return errorinfo;
                }

                string uri = "ftp://" + Path.Combine(FtpPath, fileName);

                // 根據uri創建FtpWebRequest對象
                reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri));

                // 指定數據傳輸類型
                reqFTP.UseBinary = true;

                // ftp用戶名和密碼
                reqFTP.Credentials = new NetworkCredential(Login, Password);

                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

                Stream ftpStream = response.GetResponseStream();

                long cl = response.ContentLength;

                int bufferSize = 2048;

                int readCount;

                byte[] buffer = new byte[bufferSize];

                readCount = ftpStream.Read(buffer, 0, bufferSize);

                FileStream outputStream = new FileStream(newFileName, FileMode.Create);

                while (readCount > 0)
                {

                    outputStream.Write(buffer, 0, readCount);

                    readCount = ftpStream.Read(buffer, 0, bufferSize);

                }

                ftpStream.Close();

                outputStream.Close();

                response.Close();

                //Successinfo

                return string.Format("服務器文件{0}已成功下載", fileName);

            }

            catch (Exception ex)

            {
                //errorinfo
                return string.Format("因{0},無法下載", ex.Message);

            }

        }

4.刪除文件

        public static string DeleteFile(string fileName, string FtpPath, string Login, string Password)
        {
            try
            {
                FileInfo fileInf = new FileInfo(fileName);

                string uri = "ftp://" + Path.Combine(FtpPath, fileInf.Name);

                // 根據uri創建FtpWebRequest對象
                reqFTP = (FtpWebRequest)WebRequest.Create(new Uri(uri));

                // 指定數據傳輸類型
                reqFTP.UseBinary = true;

                // ftp用戶名和密碼
                reqFTP.Credentials = new NetworkCredential(Login, Password);

                // 默認為true,連接不會被關閉

                // 在一個命令之后被執行

                reqFTP.KeepAlive = false;

                // 指定執行什么命令

                reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;

                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

                response.Close();

                //Successinfo

                return string.Format("文件{0}已成功刪除", fileInf.Name);
            }

            catch (Exception ex)
            {
                //ErrorInfo 
                return string.Format("文件因{0},無法刪除", ex.Message);
            }

        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM