将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; }