/// <summary> /// 得到文件列表 /// </summary> /// <returns></returns> public string[] GetList(string strPath) { if (ftp == null) { ftp = this.getFtpClient(); } ftp.Connect(); ftp.ChDir(strPath); return(ftp.Dir("*")); }
[TestMethod]//功能弱 public void MyFtpTest() { //string host = "121.15.166.119"; //string userName = "******"; //string userPwd = "0_1@32&*s+udd~QhfsVfEW4*5<)"; //var ftp = new FTPClient(host, 22); string host = "115.159.186.113"; string userName = "******"; string userPwd = "Djy123456Djy"; var ftp = new FTPClient(host, 21); ftp.ControlEncoding = Encoding.GetEncoding("GBK");//设置编码 ftp.Login(userName, userPwd); ftp.ConnectMode = FTPConnectMode.PASV; ftp.TransferType = FTPTransferType.BINARY; string[] mm = ftp.Features(); //获取文件列表 string[] files = ftp.Dir(); //上传文件 //ftp.Put("error.txt", "error.txt"); ftp.ChDir(files[1]);// ("61_平安银行银企直连接入规范文档(对客户)"); files = ftp.Dir(); //下载文件 // ftp.Get(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "端口转发.exe"); string strFile = files[12]; //var bb = ftp.Get(strFile); MemoryStream ms = new MemoryStream(); ftp.Get(ms, strFile); byte[] bb = ms.ToArray(); //删除文件 //ftp.Delete("Demo.cs"); ftp.Quit(); }
protected void Page_Load(object sender, EventArgs e) { //SpiderLib.DownloadFtp ftl = new DownloadFtp("ftp://172.16.32.189:21/123.txt", "imyang", ""); //Response.Write("<br/><br/><br/><strong>已经下载了FTP返回信息FtpWebResponse</strong>"); SpiderLib.FTPClient ft = new FTPClient("172.16.32.158", "/", "Anonymous", "", 21); ft.Connect(); foreach (string str in ft.Dir("*.rar")) { Response.Write(str.ToString() + "<br/>"); } }
private void ImageUploadForm_Load(object sender, EventArgs e) { //每天在服务器创建一个文件夹 string DirectoryName = PaCSGlobal.GetServerDateTime(2); string[] str = ftpClient.Dir(""); bool exists = ((IList)str).Contains(DirectoryName + "\r"); if (!exists)//如果ftp服务器上没有创建,则创建新文件夹 { ftpClient.MkDir(DirectoryName); } ftpClient.ChDir(DirectoryName); }
public void FtpTest() { FTPClient ftpClient = new FTPClient("127.0.0.1"); ftpClient.Mode = FtpMode.Passive; ftpClient.Connect(); ftpClient.Login("admin", "123"); ftpClient.SendCommand("SYST", true); ftpClient.SendCommand("FEAT", true); ftpClient.SendCommand("CLNT 1.0.0.0", true); ftpClient.SendCommand("OPTS UTF8 ON", true); ftpClient.SendCommand("PWD", true); ftpClient.SetCurrentDirectory("test"); ftpClient.Dir(); ftpClient.SendFile(@"C:\test.zip", "aa.zip", FtpType.Binary); ftpClient.GetFile("aa.zip", @"D:\test.zip", FtpType.Binary); ftpClient.MoveFile("aa.zip", "test"); }
public void Test() { using (FTPClient ftpClient = new FTPClient("127.0.0.1")) { ftpClient.Mode = FtpMode.Passive; ftpClient.Connect("127.0.0.1"); ftpClient.Login("admin", "123"); ftpClient.SendCommand("SYST", true); //ftpClient.SendCommand("FEAT",true); //ftpClient.SendCommand("CLNT 1.0.0.0",true); //ftpClient.SendCommand("OPTS UTF8 ON",true); ftpClient.SendCommand("PWD", true); //ftpClient.SetCurrentDirectory("/myphp"); string[] strings = ftpClient.Dir(""); foreach (string s in strings) { ftpClient.GetFile(s, "D:\\" + s); } //ftpClient.SendFile(@"C:\test.zip", "aa.zip", FtpType.Binary); //ftpClient.GetFile("aa.zip", @"D:\test.zip", FtpType.Binary); //ftpClient.MoveFile("aa.zip", "test"); } }
/// <summary> /// Test harness /// </summary> public static void Main(string[] args) { // we want remote host, user name and password if (args.Length < 3) { Usage(); System.Environment.Exit(1); } Logger log = Logger.GetLogger(typeof(Demo)); // assign args to make it clear string host = args[0]; string user = args[1]; string password = args[2]; Logger.CurrentLevel = Level.ALL; FTPClient ftp = null; try { // set up client log.Info("Connecting"); ftp = new FTPClient(host); // login log.Info("Logging in"); ftp.Login(user, password); // set up passive ASCII transfers log.Debug("Setting up passive, ASCII transfers"); ftp.ConnectMode = FTPConnectMode.PASV; ftp.TransferType = FTPTransferType.ASCII; // get directory and print it to console log.Debug("Directory before put:"); string[] files = ftp.Dir(".", true); for (int i = 0; i < files.Length; i++) { log.Debug(files[i]); } // copy file to server log.Info("Putting file"); ftp.Put("Demo.cs", "Demo.cs"); // get directory and print it to console log.Debug("Directory after put"); files = ftp.Dir(".", true); for (int i = 0; i < files.Length; i++) { log.Debug(files[i]); } // copy file from server log.Info("Getting file"); ftp.Get("Demo.cs.copy", "Demo.cs"); // delete file from server log.Info("Deleting file"); ftp.Delete("Demo.cs"); // get directory and print it to console log.Debug("Directory after delete"); files = ftp.Dir("", true); for (int i = 0; i < files.Length; i++) { log.Debug(files[i]); } // Shut down client log.Info("Quitting client"); ftp.Quit(); log.Info("Test complete"); } catch (Exception e) { log.Debug(e.Message, e); } }
private void textBox2_TextChanged(object sender, EventArgs e) { _loadingUserControl.Message = "正在获取FTPServer文件列表..."; _loadingUserControl.Show(this); Task.Run(() => { try { var filePath = string.Empty; textBox2.Invoke(new Action(() => { filePath = textBox2.Text; })); var list = _client.Dir(filePath, FTP.Model.DirType.MLSD); if (list != null && list.Any()) { List <ListInfo> listInfos = new List <ListInfo>(); foreach (var item in list) { if (!string.IsNullOrEmpty(item)) { var arr = item.Split(";", StringSplitOptions.RemoveEmptyEntries); if (arr.Length >= 3) { var fileName = arr[2].Trim(); var type = (arr[0] == "type=dir" ? "文件夹" : "文件"); var size = 0L; if (type == "文件") { fileName = arr[3].Trim(); size = _client.FileSize(fileName); } listInfos.Add(new ListInfo() { FileName = fileName, Type = type, Size = size }); } } } listInfos = listInfos.OrderBy(b => b.FileName).ToList(); dataGridView2.BeginInvoke(new Action(() => { dataGridView2.DataSource = null; dataGridView2.DataSource = listInfos; })); } else { dataGridView2.BeginInvoke(new Action(() => { dataGridView2.DataSource = null; })); } } catch (Exception ex) { Log("初始化ftpserver列表失败", ex.Message); } finally { _loadingUserControl.Hide(this); } }); }
/// <summary> /// Test harness /// </summary> public static void Main(string[] args) { // we want remote host, user name and password if (args.Length < 3) { Usage(); System.Environment.Exit(1); } Logger log = Logger.GetLogger(typeof(Demo)); // assign args to make it clear string host = args[0]; string user = args[1]; string password = args[2]; Logger.CurrentLevel = Level.ALL; FTPClient ftp = null; try { // set up client log.Info("Connecting"); ftp = new FTPClient(host); // login log.Info("Logging in"); ftp.Login(user, password); // set up passive ASCII transfers log.Debug("Setting up passive, ASCII transfers"); ftp.ConnectMode = FTPConnectMode.PASV; ftp.TransferType = FTPTransferType.ASCII; // get directory and print it to console log.Debug("Directory before put:"); string[] files = ftp.Dir(".", true); for (int i = 0; i < files.Length; i++) log.Debug(files[i]); // copy file to server log.Info("Putting file"); ftp.Put("readme.txt", "readme.txt"); // get directory and print it to console log.Debug("Directory after put"); files = ftp.Dir(".", true); for (int i = 0; i < files.Length; i++) log.Debug(files[i]); // copy file from server log.Info("Getting file"); ftp.Get("readme.txt.copy", "readme.txt"); // delete file from server log.Info("Deleting file"); ftp.Delete("readme.txt"); // get directory and print it to console log.Debug("Directory after delete"); files = ftp.Dir("", true); for (int i = 0; i < files.Length; i++) log.Debug(files[i]); // Shut down client log.Info("Quitting client"); ftp.Quit(); log.Info("Test complete"); } catch (Exception e) { log.Debug(e.StackTrace); } }
private void textBox2_TextChanged(object sender, EventArgs e) { _loadingUserControl.Message = "正在获取FTPServer文件列表..."; Log("正在获取FTPServer文件列表..."); _loadingUserControl.Show(this); Task.Run(() => { try { var filePath = string.Empty; textBox2.Invoke(new Action(() => { filePath = textBox2.Text; })); var list = _client.Dir(filePath, FTP.Model.DirType.MLSD); Log("已获取FTPServer文件列表"); if (list != null && list.Any()) { List <ListInfo> listInfos = new List <ListInfo>(); foreach (var item in list) { try { if (!string.IsNullOrEmpty(item)) { var arr = item.Split(";", StringSplitOptions.RemoveEmptyEntries); if (arr.Length >= 3) { var fileName = arr[2].Trim(); var type = (arr[0] == "type=dir" ? "文件夹" : "文件"); var size = 0L; if (type == "文件") { fileName = arr[3].Trim(); var sizeStr = arr[2].Substring(arr[2].IndexOf("=") + 1); long.TryParse(sizeStr, out size); } listInfos.Add(new ListInfo() { FileName = fileName, Type = type, Size = size }); } } } catch (Exception ex) { Log("获取FTPServer文件列表出现异常", ex.Message); } } listInfos = listInfos.OrderBy(b => b.FileName).ToList(); listView2.BeginInvoke(new Action(() => { listView2.Clear(); _driverHelper.GetRemoteDirectoryAndFile(listInfos); })); } else { listView2.BeginInvoke(new Action(() => { listView2.Clear(); })); } } catch (Exception ex) { Log("初始化ftpserver列表失败", ex.Message); } finally { _loadingUserControl.Hide(this); } }); }
/**/ /// <summary> /// 在下载结束后清空程序目录的多余文件 /// </summary> private static void ClearFolder() { string folder = Environment.CurrentDirectory; string[] dictorys = Directory.GetFiles(folder); foreach (string dictory in dictorys) { FileInfo info = new FileInfo(dictory); if (info.Length == 0) File.Delete(dictory); } } /**/ /// <summary> /// 递归获取ftp文件夹的内容 /// </summary> /// <param name="fileMark">文件标记</param> /// <param name="path">远程路径</param> /// <param name="client"></param> /// <param name="folder"></param> private static void GetFolder(string fileMark, string path, FTPClient client, string folder, DateTime currentDate) { string[] dirs = client.Dir(path); //获取目录下的内容 client.ChDir(path); //改变目录 foreach (string dir in dirs) { string[] infos = dir.Split(' '); //string info = infos[infos.Length - 1].Replace("\r", ""); string info = getFileName(dir.Replace("\r", "")); if (dir.StartsWith("d") && !string.IsNullOrEmpty(dir)) //为目录 { if (!info.EndsWith(".") && !info.EndsWith("..")) //筛选出真实的目录 { Directory.CreateDirectory(folder + "\\" + info); GetFolder(fileMark, path + "/" + info, client, folder + "\\" + info, currentDate); client.ChDir(path); } } else if (dir.StartsWith("-r")) //为文件 { string file = folder + "\\" + info; if (File.Exists(file)) { long remotingSize = client.GetFileSize(info); FileInfo fileInfo = new FileInfo(file); long localSize = fileInfo.Length; if (remotingSize != localSize) //短点续传 { client.GetBrokenFile(info, folder, info, localSize); } } else { //由于FTP有滞后,所以需要多取一天的数据 if (info.StartsWith("BRK.PCNT.Site Name." + currentDate.ToString("yyMMdd")) || info.StartsWith("BRK.PCNT.Site Name." + currentDate.AddDays(1).ToString("yyMMdd"))) { client.GetFile(info, folder, info); //下载文件 Console.WriteLine("文件" + folder + info + "已经下载"); } } } } }
public IList <MailboxItem> Mailbox() { return(MailboxItem.Parse(_client.Dir("mb"))); }