예제 #1
0
 public static void MakeDir(string dirPath)
 {
     if (!FTPUtils.FtpDirExists(dirPath))
     {
         try {
             FtpWebRequest  ftp      = FTPUtils.GetRequest(dirPath, WebRequestMethods.Ftp.MakeDirectory);
             FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
             response.Close();
         }
         catch (Exception ex) {
             Console.WriteLine("创建文件失败,原因: " + ex.Message);
         }
     }
 }
예제 #2
0
        public static bool FtpDirExists(string dirPath)
        {
            FtpWebRequest  reqFtp = FTPUtils.GetRequest(dirPath, WebRequestMethods.Ftp.ListDirectory);
            FtpWebResponse resFtp = null;

            try {
                resFtp = (FtpWebResponse)reqFtp.GetResponse();
                FtpStatusCode code = resFtp.StatusCode;//OpeningData
                resFtp.Close();
                return(true);
            }
            catch {
                if (resFtp != null)
                {
                    resFtp.Close();
                }
                return(false);
            }
        }
예제 #3
0
        public static void UploadingFile(string filePath, string ftpPath)
        {
            var      url      = ftpPath;
            FileInfo fileInfo = new FileInfo(filePath);
            var      username = FTPUtils.UserName;
            var      password = FTPUtils.Password;

            FTPUtils.CheckDir(ftpPath);

            FtpWebRequest reqFtp = FTPUtils.GetRequest(url, WebRequestMethods.Ftp.UploadFile, username, password);

            reqFtp.ContentLength = fileInfo.Length;

            //缓冲大小设置为2KB
            const int BufferSize = 2048;

            byte[] content = new byte[BufferSize];
            int    contentLen;
            bool   success = true;

            using (FileStream fs = fileInfo.OpenRead()) {
                try {
                    using (Stream rs = reqFtp.GetRequestStream()) {
                        contentLen = fs.Read(content, 0, BufferSize);
                        while (contentLen != 0)
                        {
                            rs.Write(content, 0, contentLen);
                            contentLen = fs.Read(content, 0, BufferSize);
                        }
                    }
                }
                catch (Exception ex) {
                    success = false;
                    Console.WriteLine(ex.Message);
                }
                finally {
                    Console.WriteLine(fileInfo.FullName + (success ? " 上传成功" : " 上传失败"));
                    reqFtp = null;
                }
            }
        }