private static void Open() { Close(); if (commandParams.Count == 0) { Console.WriteLine("Usage:open host [port]"); } else { string str = commandParams[0]; int result = 0; if (commandParams.Count == 1) { commandParams.Add("21"); } if (!int.TryParse(commandParams[1], out result)) { Console.WriteLine("unknow port!"); } else { client = new System.Net.FtpClient.FtpClient(); client.Host = str; client.Port = result; client.DataConnectionType = FtpDataConnectionType.PASV; client.Connect(); client.ValidateCertificate += new FtpSslValidation(Program.client_ValidateCertificate); } } }
/// <summary> /// 登陆 /// </summary> /// <returns>登陆是否成功</returns> public bool Login() { using (var ftp = new System.Net.FtpClient.FtpClient()) { ftp.Host = ServerHost; ftp.Credentials = new NetworkCredential(UserName, UserPassword); ftp.Connect(); return(ftp.IsConnected); } }
private System.Net.FtpClient.FtpClient CreateFtpClient() { var ftpClient = new System.Net.FtpClient.FtpClient { Host = ServerHost, Credentials = new NetworkCredential(UserName, UserPassword) }; ftpClient.Connect(); return(ftpClient); }
static void Main(string[] args) { //var ftpClient = new System.Net.FtpClient.FtpClient(); //ftpClient.Host = "192.168.1.80"; //ftpClient.Port = 21; //ftpClient. var ftpClient = new System.Net.FtpClient.FtpClient(); ftpClient.Host = "192.168.147.133"; ftpClient.Port = 9904; ftpClient.Credentials = new NetworkCredential("kerry", "P@$$w0rd"); ftpClient.UngracefullDisconnection = true; ftpClient.Connect(); //var localFile = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{Guid.NewGuid().ToString("N")}.text")); //using (var fs = localFile.Create()) //{ // var content = Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); // fs.Write(content, 0, content.Length); // fs.Flush(); //} var localFile = new FileInfo(@"F:\Users\Administrator\Desktop\com.action98.drmp.mobilemonitor.apk"); var remotePath = @"\A\com.action98.drmp.mobilemonitor.apk"; //构建异步参数 var asyncState = new Dictionary <string, object>(); asyncState.Add("LocalPath", localFile.FullName); asyncState.Add("FtpClient", ftpClient); asyncState.Add("RemotePath", remotePath); //开始异步上传 ftpClient.BeginOpenWrite(remotePath, FtpDataType.Binary, BeginOpenWriteCallback, asyncState); Console.ReadKey(); }