Beispiel #1
0
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileName"></param>
        public void Delete(string fileName)
        {
            try
            {
                string        uri = ftpURI + fileName;
                FtpWebRequest reqFTP;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.KeepAlive   = false;
                reqFTP.Method      = WebRequestMethods.Ftp.DeleteFile;

                string result = String.Empty;
                //reqFTP.Proxy = WebRequest.DefaultWebProxy;
                reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
                FtpWebResponse response   = (FtpWebResponse)reqFTP.GetResponse();
                long           size       = response.ContentLength;
                Stream         datastream = response.GetResponseStream();
                StreamReader   sr         = new StreamReader(datastream);
                result = sr.ReadToEnd();
                sr.Close();
                datastream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                WriteTraceLog.Error("FtpWeb: Delete Error --> " + ex.Message + "  文件名:" + fileName);
                throw new Exception("FtpWeb: Delete Error --> " + ex.Message + "  文件名:" + fileName);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 上传
        /// </summary>
        /// <param name="filename"></param>
        public bool Upload(string filename)
        {
            bool          bl      = false;
            FileInfo      fileInf = new FileInfo(filename);
            string        uri     = ftpURI + fileInf.Name;
            FtpWebRequest reqFTP;

            reqFTP               = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials   = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.KeepAlive     = false;
            reqFTP.Method        = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary     = true;
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 2048;

            byte[]     buff = new byte[buffLength];
            int        contentLen;
            FileStream fs   = fileInf.OpenRead();
            Stream     strm = default(Stream);

            try
            {
                //reqFTP.Proxy = WebRequest.DefaultWebProxy;
                // reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
                using (strm = reqFTP.GetRequestStream())
                {
                    contentLen = fs.Read(buff, 0, buffLength);
                    while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffLength);
                    }
                    strm.Close();
                    fs.Close();
                }
                bl = true;
            }
            catch (Exception ex)
            {
                WriteTraceLog.Error("FtpWeb: Upload Error --> " + ex.Message);
                bl = false;
                // msg = "Upload Error --> " + ex.Message;
                //  throw new Exception(string.Format("FTP Upload Error -->{0}", ex.Message));
            }

            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
                if (strm != null)
                {
                    strm.Close();
                }
            }
            return(bl);
        }
Beispiel #3
0
        /// <summary>
        /// 下载
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="fileName"></param>
        public void Download(string filePath, string fileName)
        {
            FtpWebRequest reqFTP;

            try
            {
                FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

                reqFTP             = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));
                reqFTP.Method      = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary   = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                //reqFTP.Proxy = WebRequest.DefaultWebProxy;
                reqFTP.Proxy = GlobalProxySelection.GetEmptyWebProxy();
                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);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }

                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                WriteTraceLog.Error("FtpWeb: Upload Error --> " + ex.Message);
                throw new Exception(string.Format("FTP Upload Error -->{0}", ex.Message));
            }
        }