FTP ( System.Net.FtpClient ) Upload 方法
app.conf |
<?xml version="1.0"?> <configuration> <appSettings> <add key="Host" value="<FTP Server Host>" /> <add key="FTP_User" value="<FTP User ID>" /> <add key="FTP_Passwd" value="<FTP User Passwd>" /> </appSettings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </startup> </configuration> |
Program.cs |
using System; using System.Net; using System.IO; using System.Net.FtpClient; using System.Configuration; namespace FTPClient { class Program { static void Main(string[] args) { if(args.Length < 2) { if (args.Length < 1) Console.WriteLine("Paramter : \"Local File Path\" is not existed."); if (args.Length < 2) Console.WriteLine("Paramter : \"FTP Destination Folder Name\" is not existed."); Console.WriteLine("ConsoleApplication1.exe [Local File Path] [FTP Destination Folder Name]"); } else if (args.Length == 2 && args[0] != null && args[1] != null) { FileInfo file = new FileInfo(args[0].Trim()); if (file.Exists) { Console.WriteLine("Local File Path : " + file.FullName.Trim() + " Existed"); uploadtest(file, args[1]); } else { Console.WriteLine("Local File Path : " + file.FullName.Trim() + " Not Existed"); } } Console.ReadLine(); } private static void uploadtest(FileInfo LocalFilePath, String FolderPath) { try { using (var client = new System.Net.FtpClient.FtpClient()) { String Host = ConfigurationSettings.AppSettings["Host"]; String FTP_User = ConfigurationSettings.AppSettings["FTP_User"]; String FTP_Passwd = ConfigurationSettings.AppSettings["FTP_Passwd"]; client.Host = Host; client.Credentials = new NetworkCredential(FTP_User, FTP_Passwd); client.DataConnectionType = System.Net.FtpClient.FtpDataConnectionType.PORT; //client.DataConnectionType = System.Net.FtpClient.FtpDataConnectionType.PASV; Stream stream; if (client.DirectoryExists(FolderPath.Trim())) { Console.WriteLine("FTP Destination Folder Name : \n\"" + FolderPath.Trim() + "\" Existed on " + Host + " FTP Server\n"); Console.WriteLine("Press Any Key to start Uploading – File : \n" + LocalFilePath.FullName.Trim() + "\n"); Console.ReadLine(); //for (int i = 0; i < 20; i++) //{ using (stream = new FileStream(LocalFilePath.FullName.Trim(), FileMode.Open, FileAccess.Read)) { using (var outStream = client.OpenWrite(FolderPath.Trim() + @"\" + LocalFilePath.Name.Trim(), System.Net.FtpClient.FtpDataType.Binary)) { byte[] buffer = new byte[32768]; int read; long uploadsize = 0; while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) { outStream.Write(buffer, 0, read); uploadsize += read; Console.Clear(); Console.WriteLine("FTP Destination Folder Name : \n\"" + FolderPath.Trim() + "\" Folder Existed on " + Host + " FTP Server\n"); Console.WriteLine("Start Uploading – File : \n" + LocalFilePath.FullName.Trim() + "\n"); Console.WriteLine("File Size : " + uploadsize.ToString() + "/" + LocalFilePath.Length.ToString() + "\n"); Console.WriteLine("Upload Progress : " + (long)(((double)uploadsize / (double)LocalFilePath.Length) * 100) + "%\n"); } } } Console.WriteLine("Complete Uploading – File : " + LocalFilePath.Name.Trim()); //} } else { Console.WriteLine("FTP Destination Folder Name : \"" + FolderPath.Trim() + "\" Not Existed or Access Denied."); } } }catch(Exception ex){ Console.WriteLine(ex.Message); } } } } |