Namespace used:
Sample Code:
Function can be used as
- using System.Net;
- using System.IO;
Sample Code:
- public void ftpfile(string ftpfilepath, string inputfilepath)
- {
- string ftphost = "127.0.0.1";
- //here correct hostname or IP of the ftp server to be given
- string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
- FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
- ftp.Credentials = new NetworkCredential("userid", "password");
- //userid and password for the ftp server to given
- ftp.KeepAlive = true;
- ftp.UseBinary = true;
- 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();
- }
Function can be used as
- ftpfile(@"/testfolder/testfile.xml", @"c:\testfile.xml");
Reference :http://blog.logiclabz.com/c/ftp-file-upload-using-ftpwebrequest-in-net-c.aspx
Code to check Directory exists in ftp or not
private void btnCheck_Click(object sender, EventArgs e)
{
bool result = FtpDirectoryExists("ftp://micro123/First", "anonymous", "anonymous");
MessageBox.Show("Folder"+result);
}
public bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)
{
bool IsExists = true;
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
IsExists = false;
}
return IsExists;
}