/// <summary> /// 得到文件列表 /// </summary> /// <returns></returns> public string[] GetList(string strPath) { if (ftp == null) { ftp = this.getFtpClient(); } ftp.Connect(); ftp.ChDir(strPath); return(ftp.Dir("*")); }
public void Connect() { _client.Connect(); _client.Login(_user, _password); _client.TransferType = FTPTransferType.ASCII; _client.ConnectMode = FTPConnectMode.ACTIVE; }
/// <summary>Connect to the FTP server using configuration parameters</summary> /// <returns>An FTPClient instance</returns> /// <exception cref="System.IO.IOException"/> private FTPClient Connect() { FTPClient client = null; Configuration conf = GetConf(); string host = conf.Get(FsFtpHost); int port = conf.GetInt(FsFtpHostPort, FTP.DefaultPort); string user = conf.Get(FsFtpUserPrefix + host); string password = conf.Get(FsFtpPasswordPrefix + host); client = new FTPClient(); client.Connect(host, port); int reply = client.GetReplyCode(); if (!FTPReply.IsPositiveCompletion(reply)) { throw NetUtils.WrapException(host, port, NetUtils.UnknownHost, 0, new ConnectException ("Server response " + reply)); } else { if (client.Login(user, password)) { client.SetFileTransferMode(FTP.BlockTransferMode); client.SetFileType(FTP.BinaryFileType); client.SetBufferSize(DefaultBufferSize); } else { throw new IOException("Login failed on server - " + host + ", port - " + port + " as user '" + user + "'"); } } return(client); }
private void btnLogin_Click(object sender, EventArgs e) { try { if (btnLogin.Text == "点击连接") { if (CheckUserInfo()) { string ipAddr = this.toolStripTextBoxIpAddr.Text.Trim(); string port = this.toolStripTextBoxPort.Text.Trim(); string userName = this.toolStripTextBoxName.Text.Trim(); string password = this.toolStripTextBoxPassword.Text.Trim(); ftpClient = new FTPClient(ipAddr, port, userName, password); if (!ftpClient.Connect()) { lblMsg.Text = "连接失败,请检查服务器状况"; } else { if (ftpClient.Login()) { lblMsg.Text = "登录成功"; btnLogin.Text = "断开连接"; toolStripTextBoxIpAddr.ReadOnly = true; toolStripTextBoxName.ReadOnly = true; toolStripTextBoxPassword.ReadOnly = true; toolStripTextBoxPort.ReadOnly = true; ShowFilesDirectory(); } else { lblMsg.Text = "用户名/密码出错"; ftpClient.Close(); } } } } else { ftpClient.Close(); FTPflowLayoutPanel.Controls.Clear(); btnLogin.Text = "点击连接"; toolStripTextBoxIpAddr.ReadOnly = false; toolStripTextBoxName.ReadOnly = false; toolStripTextBoxPassword.ReadOnly = false; toolStripTextBoxPort.ReadOnly = false; lblMsg.Text = ""; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
protected override bool DocumentProcessing() { if (File.Exists(dataFileName)) { FTPClient ftp = new FTPClient( _task.Ftp_Address, _task.Ftp_Port, _task.Ftp_UserID, _task.Ftp_UserPWD); InvokeWriteLog(string.Format("连接 FTP 服务器:{0}", _task.Ftp_Address)); try { ftp.Connect(); } catch (Exception error) { InvokeWriteLog(string.Format("连接 FTP 服务器时发生错误:{0}", error.Message)); return(false); } try { long intRlt = 0; InvokeWriteLog(string.Format("开始上传文件[{0}]", dataFileName)); string strDestinationFileName = Path.GetFileName(dataFileName); ftp.OpenUpload(dataFileName, strDestinationFileName); while ((intRlt = ftp.DoUpload()) > 0) { Thread.Sleep(10); } if (intRlt == -1) { InvokeWriteLog(string.Format("上传文件时发生错误:{0}", ftp.errormessage)); return(false); } else { InvokeWriteLog("文件上传完毕"); } } finally { InvokeWriteLog("断开服务器连接"); ftp.Disconnect(); } } else { InvokeWriteLog(string.Format("文件[{0}]不存在,无法处理", dataFileName)); } return(true); }
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 AnalyzeFtpRunning() { try { FTPClient ftpClient = new FTPClient(localIP, "/", "anonymous", "", 21); ftpClient.Connect(); } catch (Exception ex) { MessageUtil.ShowError("本机接收软件可能没有运行!"); return; } MessageUtil.ShowInformation("本地电脑接收没有发现问题!"); }
private void button3_Click(object sender, EventArgs e) { FTPClient FTPClient = new FTPClient(txtIP.Text.Trim(), @"CMM\0001\0001-C403-D02", txtUse.Text.Trim(), txtPW.Text.Trim(), int.Parse(txtport.Text)); FTPClient.Connect(); if (FTPClient.Connected) { //首先进入到目标目录 //FTPClient.ChDir(@"MachRoders\160725"); //FTPClient.RmDir(new DirectoryInfo(@"F:\EACT3.0\EACT3.0\UI\bin\Debug\CNCMACHINE\10000081").Name); FTPClient.Get("0001-C403-D02.stp", @"C:\Users\cj\Desktop\三亚消防", "0001-C403-D02.stp"); } FTPClient.DisConnect(); MessageBox.Show("OK"); }
private void TcpD_Click(object sender, EventArgs e) { FolderBrowserDialog FB = new FolderBrowserDialog(); FB.Description = "选择将文件夹保存到本地文件夹"; if (FB.ShowDialog() == System.Windows.Forms.DialogResult.OK) { FTPClient FTPClient = new FTPClient("192.168.1.30", "", "eact", "123", 21); FTPClient.Connect(); if (FTPClient.Connected) { var ee = FTPClient.List("-a"); } FTPClient.DisConnect(); MessageBox.Show("OK"); } }
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"); }
private void TcpU_Click(object sender, EventArgs e) { FolderBrowserDialog FB = new FolderBrowserDialog(); FB.Description = "选择需要上传的文件夹"; if (FB.ShowDialog() == System.Windows.Forms.DialogResult.OK) { FTPClient FTPClient = new FTPClient(txtIP.Text.Trim(), "", txtUse.Text.Trim(), txtPW.Text.Trim(), 21); FTPClient.Connect(); if (FTPClient.Connected) { //首先进入到目标目录 //FTPClient.ChDir(@"MachRoders\160725"); //FTPClient.RmDir(new DirectoryInfo(@"F:\EACT3.0\EACT3.0\UI\bin\Debug\CNCMACHINE\10000081").Name); FTPClient.Put(FB.SelectedPath, "*"); } FTPClient.DisConnect(); MessageBox.Show("OK"); } }
private void button2_Click(object sender, EventArgs e) { OpenFileDialog FB = new OpenFileDialog(); FB.Multiselect = false; if (FB.ShowDialog() == System.Windows.Forms.DialogResult.OK) { FTPClient FTPClient = new FTPClient(txtIP.Text.Trim(), "", txtUse.Text.Trim(), txtPW.Text.Trim(), int.Parse(txtport.Text)); FTPClient.Connect(); if (FTPClient.Connected) { //首先进入到目标目录 //FTPClient.ChDir(@"MachRoders\160725"); //FTPClient.RmDir(new DirectoryInfo(@"F:\EACT3.0\EACT3.0\UI\bin\Debug\CNCMACHINE\10000081").Name); FTPClient.Put(FB.FileName); } FTPClient.DisConnect(); MessageBox.Show("OK"); } }
/// <summary> /// 上传 /// </summary> private void Upload() { ftpClient = new FTPClient(FTPAddress, FTPUserName, FTPPassword, FTPModel.Binary, Encoding.Default); int result = ftpClient.Connect(); if (result == 0) { ftpClient.OnProgressChanged += ftpClient_OnProgressChanged; ftpClient.OnCompleted += ftpClient_OnCompleted; ftpClient.OnFailed += ftpClient_OnFailed; FtpResult = ftpClient.Upload(LocalPath, FTPAddress, ServerUploadPath); // 开始上传 } else { this.Dispatcher.BeginInvoke(new Action(() => { ProgressBar.Value = -1; })); } }
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"); } }
private void skinButton1_Click(object sender, EventArgs e) { if (skinButton1.Text == "Connect") { _loadingUserControl.Message = "正在连接到FTPServer..."; _loadingUserControl.Show(this); groupBox1.Enabled = false; splitContainer2.Panel2.Enabled = false; try { var ip = skinWaterTextBox1.Text; var port = int.Parse(skinWaterTextBox2.Text); var username = skinWaterTextBox3.Text; var pwd = skinWaterTextBox4.Text; _client = new FTPClient(ip, port, username, pwd); Task.Run(() => { try { _client.Connect(); Log($"连接到FTPServer {ip}:{port}成功"); splitContainer2.BeginInvoke(new Action(() => { groupBox1.Enabled = true; skinWaterTextBox1.Enabled = skinWaterTextBox2.Enabled = skinWaterTextBox3.Enabled = skinWaterTextBox4.Enabled = false; skinButton1.Enabled = true; skinButton1.Text = "DisConnect"; splitContainer2.Panel2.Enabled = true; textBox2.Text = "/"; dataGridView2.Enabled = true; })); textBox2_TextChanged(null, null); } catch (Exception ex) { MessageBox.Show("连接到FTPServer失败,ex:" + ex.Message); Log("连接到FTPServer失败", ex.Message); this.BeginInvoke(new Action(() => { groupBox1.Enabled = true; })); } finally { _loadingUserControl.Hide(this); } }); } catch (Exception ex) { _loadingUserControl.Hide(this); MessageBox.Show("连接到FTPServer失败,ex:" + ex.Message); Log("连接到FTPServer失败", ex.Message); groupBox1.Enabled = true; } finally { _loadingUserControl.Hide(this); } } else { Task.Run(() => { try { _client.Quit(); } catch (Exception ex) { Log("断开FTP失败", ex.Message); } finally { skinButton1.Invoke(new Action(() => { skinWaterTextBox1.Enabled = skinWaterTextBox2.Enabled = skinWaterTextBox3.Enabled = skinWaterTextBox4.Enabled = true; skinButton1.Enabled = true; dataGridView2.Enabled = false; dataGridView2.DataSource = null; skinButton1.Text = "Connect"; })); } }); } }
public void Ftp(List <string> zipFiles) { IEnumerable <XElement> ftps = gConfig.GetElements("Backup/Ftp"); foreach (XElement xeFtp in ftps) { if (gAbortTask) { break; } string ftpServer = xeFtp.zAttribValue("server"); if (ftpServer == null) { //cTrace.Trace("Ftp : server is'nt defined"); gTaskTrace.WriteLine("Ftp : server is'nt defined"); continue; } string ftpUser = xeFtp.zAttribValue("user"); if (ftpUser == null) { //cTrace.Trace("Ftp : user is'nt defined"); gTaskTrace.WriteLine("Ftp : user is'nt defined"); continue; } string ftpPassword = xeFtp.zAttribValue("password"); string ftpDirectory = xeFtp.zAttribValue("directory"); string dir = ""; if (ftpDirectory != null) { dir = " directory " + ftpDirectory; } gTaskTrace.WriteLine("Ftp : connect to server {0}{1}", ftpServer, dir); FTPClient ftp = new FTPClient(); try { ftp.RemoteHost = ftpServer; ftp.Connect(); ftp.Login(ftpUser, ftpPassword); if (ftpDirectory != null) { ftp.ChDir(ftpDirectory); } ftp.TransferType = FTPTransferType.BINARY; ftp.BytesTransferred += new BytesTransferredHandler(FtpBytesTransferred); int iFile = 0; foreach (string zipFile in zipFiles) { if (gAbortTask) { break; } gTaskProgress.SetProgressText("Ftp copy file " + zipFile); gTaskProgress.SetProgress(++iFile, zipFiles.Count); gTaskTrace.WriteLine("Ftp : copy file {0}", zipFile); if (!zFile.Exists(zipFile)) { //cTrace.Trace("Ftp : file does'nt exist {0}", zipFile); gTaskTrace.WriteLine("Ftp : file does'nt exist {0}", zipFile); continue; } string remoteFile = zPath.GetFileName(zipFile); //glFtpByteToTransfer = new FileInfo(zipFile).Length; glFtpByteToTransfer = zFile.CreateFileInfo(zipFile).Length; gTaskProgressDetail.SetProgressText("FTP transfer file {0}", remoteFile); try { ftp.Put(zipFile, remoteFile); } catch { if (!gbErrorGeneratedByFtpStop) { throw; } //string sError = cError.GetErrorMessage(ex, false, true); //gTrace.Trace(sError); break; } gTaskProgressDetail.SetProgress(glFtpByteToTransfer, glFtpByteToTransfer); } ftp.BytesTransferred -= new BytesTransferredHandler(FtpBytesTransferred); } finally { if (!gbErrorGeneratedByFtpStop) { ftp.Quit(); } else { gbErrorGeneratedByFtpStop = false; } } } }
private void GetFTPData(DateTime currentDate) { m_localFolder = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "temp/BI_CustomerFlow/FTPData/" + currentDate.ToShortDateString(); FTPClient client = new FTPClient("192.168.0.26", "", "jk", "jk654@123", 21); client.Connect(); GetFolder("*", ".ep01", client, CreateFolder(), currentDate); //由于 FTP的数据有滞后,所以需要缓冲一天的数据 //GetFolder("*", ".ep01", client, CreateFolder(), "BRK.PCNT.Site Name." + currentDate.AddDays(1).ToString("yyMMdd")); client.DisConnect(); //ClearFolder() Console.WriteLine("下载完毕"); m_dtFTP = new BI_CustomerFlow.T_CustomerFlowDataTable(); m_dtFTP.Columns.Remove("Seq_No"); //获取数据 foreach (string strFileName in Directory.GetFiles(m_localFolder)) { FileStream fs = new FileStream(strFileName, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, (Int32)fs.Length); fs.Close(); fs.Close(); fs.Close(); fs.Dispose(); fs.Dispose(); fs.Dispose(); string[] strText = Encoding.UTF8.GetString(buffer).Replace("\r\n", "\n").Split('\n'); //案例数据:25||Site ID||New Zone0|2|09/11/2013 04:00:00|1|1|160|159|192.168.254.2| foreach (string strRow in strText) { if (strRow == "") { continue; } string strTemp = ""; if (strRow.Substring(strRow.Length - 1) == "|") { strTemp = strRow.Substring(0, strRow.Length - 1); } string[] str = (System.Guid.NewGuid().ToString() + "|" + strTemp).Split('|'); //错误数据 if (str.Length != 13) { continue; } if (str[7].StartsWith(currentDate.ToString("MM/dd/yyyy"))) { m_dtFTP.Rows.Add(str); } } } }