Created
April 18, 2013 13:03
-
-
Save gopigujjula/5412532 to your computer and use it in GitHub Desktop.
File copying using FTP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Text; | |
using System.Net; | |
using System.IO; | |
namespace FtpFileCopy | |
{ | |
public class FtpFileUpload | |
{ | |
static void Main(string[] args) | |
{ | |
FtpFileUpload.ftpfileUpload(@"D:\Test\sample.xml"); | |
} | |
public static bool ftpfileUpload(string inputfilepath) | |
{ | |
string ftpMachinePath = "ftp://10.19.74.99/SAMPLE/samplecopy.xml"; | |
string ftpMachineUserName = "gopikreddy"; | |
string ftpMachinePassword = "password"; | |
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpMachinePath); | |
//userid and password for the ftp server to given | |
ftp.Credentials = new NetworkCredential(ftpMachineUserName, ftpMachinePassword); | |
ftp.KeepAlive = true; | |
ftp.UseBinary = true; | |
try | |
{ | |
ftp.Method = WebRequestMethods.Ftp.UploadFile; | |
FileStream fs = File.OpenRead(inputfilepath); | |
byte[] buffer = new byte[fs.Length]; | |
fs.Read(buffer, 0, buffer.Length); | |
fs.Close(); | |
Stream ftpstream = ftp.GetRequestStream(); | |
ftpstream.Write(buffer, 0, buffer.Length); | |
ftpstream.Close(); | |
return true; | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment