コード例 #1
0
 /// <summary>
 /// 从FTP服务器上面删除一个文件
 /// </summary>
 /// <param name="RemoteFileName">远程文件名</param>
 public void DeleteFile(string RemoteFileName)
 {
     try
     {
         if (!IsValidFileChars(RemoteFileName))
         {
             throw new Exception("文件名非法!");
         }
         Response = Open(new Uri(this.Uri.ToString() + RemoteFileName), WebRequestMethods.Ftp.DeleteFile);
         MessageConsole.WriteConsole("执行删除操作,返回状态:" + Response.StatusCode);
     }
     catch (Exception ep)
     {
         MessageConsole.WriteConsole(ep);
         ErrorMsg = ep.ToString();
         throw ep;
     }
 }
コード例 #2
0
        /// <summary>
        /// 从FTP服务器下载文件,指定本地路径和本地文件名
        /// </summary>
        /// <param name="RemoteFileName">远程文件名</param>
        /// <param name="LocalPath">本地路径</param>
        /// <param name="LocalFilePath">保存文件的本地路径,后面带有"\"</param>
        /// <param name="LocalFileName">保存本地的文件名</param>
        public bool DownloadFile(string RemoteFileName, string LocalPath, string LocalFileName)
        {
            byte[] bt = null;
            try
            {
                if (!IsValidFileChars(RemoteFileName) || !IsValidFileChars(LocalFileName) || !IsValidPathChars(LocalPath))
                {
                    throw new Exception("非法文件名或目录名!");
                }
                if (!Directory.Exists(LocalPath))
                {
                    throw new Exception("本地文件路径不存在!");
                }

                string LocalFullPath = Path.Combine(LocalPath, LocalFileName);
                if (File.Exists(LocalFullPath))
                {
                    throw new Exception("当前路径下已经存在同名文件!");
                }
                bt = DownloadFile(RemoteFileName);
                if (bt != null)
                {
                    FileStream stream = new FileStream(LocalFullPath, FileMode.Create);
                    stream.Write(bt, 0, bt.Length);
                    stream.Flush();
                    stream.Close();
                    MessageConsole.WriteConsole("下载文件“" + RemoteFileName + "”成功,本地路径:“" + LocalPath + "”,本地文件名:“" + LocalFileName + "”。");
                    return(true);
                }
                else
                {
                    MessageConsole.WriteConsole("下载“" + RemoteFileName + "”失败,本地路径:“" + LocalPath + "”,本地文件名:“" + LocalFileName + "”。", FTPModel.LogModel.LogLevel.Error);
                    return(false);
                }
            }
            catch (Exception ep)
            {
                MessageConsole.WriteConsole(ep);
                ErrorMsg = ep.ToString();
                throw ep;
            }
        }