将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