狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

【C#】FTPHelper-封裝FTP的相關操作

admin
2021年5月11日 17:32 本文熱度 2597

using System;

using System.IO;

using System.Net;

using System.Text;

 

namespace Whir.Software.DataSyncTools.Library.Helper

{

    /// <summary>

    ///     Ftp輔助類

    /// </summary>

    public class FtpHelper

    {

        private const int BufferSize = 2048;

        private readonly string _host;

        private readonly string _pass;

        private readonly string _user;

        private FtpWebRequest _ftpRequest;

        private FtpWebResponse _ftpResponse;

        private Stream _ftpStream;

 

        public FtpHelper(string hostIp, string userName, string password)

        {

            _host = hostIp;

            _user = userName;

            _pass = password;

        }

 

        /// <summary>

        ///     下載文件

        /// </summary>

        /// <param name="localFile"></param>

        /// <param name="remoteFile"></param>

        /// <returns></returns>

        public FtpResult Download(string localFile, string remoteFile)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + remoteFile);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                var localFileStream = new FileStream(localFile, FileMode.Create);

                var byteBuffer = new byte[BufferSize];

                if (_ftpStream != null)

                {

                    int bytesRead = _ftpStream.Read(byteBuffer, 0, BufferSize);

                    try

                    {

                        while (bytesRead > 0)

                        {

                            localFileStream.Write(byteBuffer, 0, bytesRead);

                            bytesRead = _ftpStream.Read(byteBuffer, 0, BufferSize);

                        }

                    }

                    catch (Exception ex)

                    {

                        result = new FtpResult(false, ex.Message);

                        return result;

                    }

                }

                localFileStream.Close();

                if (_ftpStream != null) _ftpStream.Close();

                _ftpResponse.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     上傳文件

        /// </summary>

        /// <param name="localFile"></param>

        /// <param name="remoteFile"></param>

        /// <returns></returns>

        public FtpResult Upload(string localFile, string remoteFile)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + remoteFile);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

                _ftpStream = _ftpRequest.GetRequestStream();

                var localFileStream = new FileStream(localFile, FileMode.Create);

                var byteBuffer = new byte[BufferSize];

                int bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize);

                try

                {

                    while (bytesSent != 0)

                    {

                        _ftpStream.Write(byteBuffer, 0, bytesSent);

                        bytesSent = localFileStream.Read(byteBuffer, 0, BufferSize);

                    }

                }

                catch (Exception ex)

                {

                    result = new FtpResult(false, ex.Message);

                    return result;

                }

                localFileStream.Close();

                _ftpStream.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     刪除文件

        /// </summary>

        /// <param name="deleteFile"></param>

        public FtpResult Delete(string deleteFile)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + deleteFile);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpResponse.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     文件重命名

        /// </summary>

        /// <param name="currentFileNameAndPath"></param>

        /// <param name="newFileName"></param>

        /// <returns></returns>

        public FtpResult Rename(string currentFileNameAndPath, string newFileName)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + currentFileNameAndPath);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.Rename;

                _ftpRequest.RenameTo = newFileName;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpResponse.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     創建目錄

        /// </summary>

        /// <param name="newDirectory"></param>

        /// <returns></returns>

        public FtpResult CreateDirectory(string newDirectory)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + newDirectory);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpResponse.Close();

                _ftpRequest = null;

                result = new FtpResult(true, "ok");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     取得文件創建時間

        /// </summary>

        /// <param name="fileName"></param>

        /// <returns></returns>

        public FtpResult GetFileCreatedDateTime(string fileName)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + fileName);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                if (_ftpStream != null)

                {

                    var ftpReader = new StreamReader(_ftpStream);

                    string fileInfo;

                    try

                    {

                        fileInfo = ftpReader.ReadToEnd();

                    }

                    catch (Exception ex)

                    {

                        result = new FtpResult(false, ex.Message);

                        ftpReader.Close();

                        if (_ftpStream != null) _ftpStream.Close();

                        _ftpResponse.Close();

                        _ftpRequest = null;

                        return result;

                    }

                    ftpReader.Close();

                    if (_ftpStream != null) _ftpStream.Close();

                    _ftpResponse.Close();

                    _ftpRequest = null;

                    return new FtpResult(true, fileInfo);

                }

                return new FtpResult(false, "響應流為空");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     取得文件大小

        /// </summary>

        /// <param name="fileName"></param>

        /// <returns></returns>

        public FtpResult GetFileSize(string fileName)

        {

            FtpResult result;

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + fileName);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                if (_ftpStream != null)

                {

                    var ftpReader = new StreamReader(_ftpStream);

                    string fileInfo = null;

                    try

                    {

                        while (ftpReader.Peek() != -1)

                        {

                            fileInfo = ftpReader.ReadToEnd();

                        }

                    }

                    catch (Exception ex)

                    {

                        result = new FtpResult(false, ex.Message);

                        ftpReader.Close();

                        if (_ftpStream != null) _ftpStream.Close();

                        _ftpResponse.Close();

                        _ftpRequest = null;

                        return result;

                    }

                    ftpReader.Close();

                    _ftpStream.Close();

                    _ftpResponse.Close();

                    _ftpRequest = null;

                    return new FtpResult(true, fileInfo);

                }

                result = new FtpResult(false, "響應流為空");

            }

            catch (Exception ex)

            {

                result = new FtpResult(false, ex.Message);

            }

            return result;

        }

 

        /// <summary>

        ///     顯示遠程目錄結構

        /// </summary>

        /// <param name="directory"></param>

        /// <returns></returns>

        public string[] DirectoryListSimple(string directory)

        {

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + directory);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                if (_ftpStream != null)

                {

                    var ftpReader = new StreamReader(_ftpStream);

                    string directoryRaw = null;

                    try

                    {

                        while (ftpReader.Peek() != -1)

                        {

                            directoryRaw += ftpReader.ReadLine() + "|";

                        }

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.ToString());

                    }

                    ftpReader.Close();

                    _ftpStream.Close();

                    _ftpResponse.Close();

                    _ftpRequest = null;

                    /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */

                    try

                    {

                        if (directoryRaw != null)

                        {

                            string[] directoryList = directoryRaw.Split("|".ToCharArray());

                            return directoryList;

                        }

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.ToString());

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

            return new[] { "" };

        }

 

        /// <summary>

        ///     遠程文件列表

        /// </summary>

        /// <param name="directory"></param>

        /// <returns></returns>

        public string[] DirectoryListDetailed(string directory)

        {

            try

            {

                _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + directory);

                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);

                _ftpRequest.UseBinary = true;

                _ftpRequest.UsePassive = true;

                _ftpRequest.KeepAlive = true;

                _ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

                _ftpResponse = (FtpWebResponse)_ftpRequest.GetResponse();

                _ftpStream = _ftpResponse.GetResponseStream();

                if (_ftpStream != null)

                {

                    var ftpReader = new StreamReader(_ftpStream);

                    string directoryRaw = null;

                    try

                    {

                        while (ftpReader.Peek() != -1)

                        {

                            directoryRaw += ftpReader.ReadLine() + "|";

                        }

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.ToString());

                    }

                    ftpReader.Close();

                    _ftpStream.Close();

                    _ftpResponse.Close();

                    _ftpRequest = null;

                    try

                    {

                        if (directoryRaw != null)

                        {

                            string[] directoryList = directoryRaw.Split("|".ToCharArray());

                            return directoryList;

                        }

                    }

                    catch (Exception ex)

                    {

                        Console.WriteLine(ex.ToString());

                    }

                }

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex.ToString());

            }

            /* Return an Empty string Array if an Exception Occurs */

            return new[] { "" };

        }

    }

 

    public class FtpResult

    {

        public FtpResult(bool isCusecess, string message)

        {

            IsSucess = isCusecess;

            Message = message;

        }

 

        public bool IsSucess { get; set; }

        public string Message { get; set; }

    }

}


該文章在 2021/5/11 17:33:25 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved