將FTP上文件下載到本地


     /// <summary>
        /// 獲取FTP文檔
        /// </summary>
        /// <returns>路徑</returns>
        public static string DisplayFileFromServer(Uri serverUri, out string errorMsg,string username= "test", string userpwd= "test")
        {
            errorMsg = string.Empty;
            if (serverUri.Scheme != Uri.UriSchemeFtp)
            {
                errorMsg = "路徑非FTP";
                return null;
            }
            WebClient request = new WebClient();
            request.Credentials = new NetworkCredential(username,userpwd);
            
            try
            {
                byte[] newFileData = request.DownloadData(serverUri.ToString());

                string Path = @"D:\GeneFile\" + serverUri.Segments.Last().Split('.')[0] + "\\";
                if (!System.IO.Directory.Exists(Path))
                    System.IO.Directory.CreateDirectory(Path);

                //string newFileName = serverUri.LocalPath.Remove(0,serverUri.LocalPath.LastIndexOf('.')).Insert(0, DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss"));
                string newFileName = serverUri.Segments.Last();
                string PathUrl = Path + newFileName;
                if (File.Exists(PathUrl))
                    File.Delete(PathUrl);

                File.WriteAllBytes(PathUrl, newFileData);
                
                return PathUrl;
            }
            catch (WebException ex)
            {
                errorMsg = ex.Message;
                Log.WriteLog(ex);
            }
            return null;
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM