Esempio n. 1
0
        /// <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   = false;
                _ftpRequest.Timeout     = 1000 * 30;
                _ftpRequest.Method      = WebRequestMethods.Ftp.MakeDirectory;
                _ftpResponse            = (FtpWebResponse)_ftpRequest.GetResponse();

                Dispose();
                result = new FtpResult(true, "ok");
            }
            catch (Exception ex)
            {
                Dispose();
                result = new FtpResult(false, ex.Message);
            }
            return(result);
        }
Esempio n. 2
0
        /// <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   = false;
                _ftpRequest.Timeout     = 1000 * 30;
                _ftpRequest.Method      = WebRequestMethods.Ftp.Rename;
                _ftpRequest.RenameTo    = newFileName;
                _ftpResponse            = (FtpWebResponse)_ftpRequest.GetResponse();
                Dispose();
                result = new FtpResult(true, "ok");
            }
            catch (Exception ex)
            {
                Dispose();
                result = new FtpResult(false, ex.Message);
            }
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        ///     显示远程目录结构
        /// </summary>
        /// <param name="directory"></param>
        /// <returns></returns>
        public FtpResult DirectoryListSimple(string directory)
        {
            var result = new FtpResult();

            try
            {
                _ftpRequest             = (FtpWebRequest)WebRequest.Create(_host + "/" + directory);
                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);
                _ftpRequest.UseBinary   = true;
                _ftpRequest.UsePassive  = true;
                _ftpRequest.KeepAlive   = false;
                _ftpRequest.Timeout     = 1000 * 30;
                _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)
                    {
                        result = new FtpResult(false, ex.Message);
                    }
                    ftpReader.Close();
                    Dispose();
                    /* 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());
                            result = new FtpResult(true, directoryList);
                        }
                    }
                    catch (Exception ex)
                    {
                        Dispose();
                        result = new FtpResult(false, ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                Dispose();
                result = new FtpResult(false, ex.Message);
            }
            return(result);
        }
Esempio n. 4
0
        /// <summary>
        ///     下载文件
        /// </summary>
        /// <param name="localFile"></param>
        /// <param name="remoteFile"></param>
        /// <returns></returns>
        public FtpResult Download(string localFile, string remoteFile)
        {
            FtpResult result;

            _ftpRequest = (FtpWebRequest)WebRequest.Create(_host + "/" + remoteFile);
            try
            {
                _ftpRequest.Credentials = new NetworkCredential(_user, _pass);
                _ftpRequest.UseBinary   = true;
                _ftpRequest.UsePassive  = true;
                _ftpRequest.KeepAlive   = false;
                _ftpRequest.Timeout     = 1000 * 30;
                _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();
                Dispose();
                result = new FtpResult(true, "ok");
            }
            catch (Exception ex)
            {
                Dispose();
                result = new FtpResult(false, ex.Message);
            }
            return(result);
        }
Esempio n. 5
0
        /// <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   = false;
                _ftpRequest.Timeout     = 1000 * 30;
                _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();
                        Dispose();
                        return(result);
                    }
                    Dispose();
                    return(new FtpResult(true, fileInfo));
                }
                result = new FtpResult(false, "响应流为空");
            }
            catch (Exception ex)
            {
                Dispose();
                result = new FtpResult(false, ex.Message);
            }
            return(result);
        }