Advertisement
Guest User

FTP upload code

a guest
Apr 4th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.56 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4.                    
  5. public class Program
  6. {
  7.     public static void Main(string[] args)
  8.     {  
  9.         //settings for the program to run...
  10.         string uploadFile = string.Empty;
  11.         string FTPServer = string.Empty;
  12.         string FTPUser = string.Empty;
  13.         string FTPPassword = string.Empty;
  14.        
  15.         if(args.Length != 4) //check if the parameters were set...
  16.         {
  17.             Console.WriteLine("Not all parameters were provided, please enter fields manually...");
  18.            
  19.             Console.WriteLine("Please enter the server IP: ");
  20.             FTPServer = Console.ReadLine();
  21.            
  22.             Console.WriteLine("Please enter the FTP username: ");
  23.             FTPUser = Console.ReadLine();
  24.            
  25.             Console.WriteLine("Please enter the FTP Password: ");
  26.             FTPPassword = Console.ReadLine();
  27.            
  28.             Console.WriteLine("Please enter the file path to upload: ");
  29.             uploadFile = Console.ReadLine();
  30.         }
  31.         else
  32.         {
  33.            
  34.             //Get the command line arguements
  35.             FTPServer = args[0];
  36.             FTPUser = args[1];
  37.             FTPPassword = args[2];
  38.             uploadFile = args[3];
  39.         }
  40.        
  41.         using(FileStream fs = File.OpenRead(uploadFile))
  42.         {
  43.             FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(FTPServer + "/" + uploadFile);
  44.             ftp.Credentials = new NetworkCredential(FTPUser, FTPPassword);
  45.             ftp.Timeout =  -1; //no timeout for big files...
  46.             ftp.KeepAlive = true;
  47.             ftp.UseBinary = true;
  48.             ftp.Method = WebRequestMethods.Ftp.UploadFile;
  49.            
  50.             fs.CopyTo(ftp.GetRequestStream()); //upload the file
  51.            
  52.             //Close the stream cause the upload is done...
  53.             ftp.GetRequestStream().Close();
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement