Ftp transfer files from local to remote ftp server using c#
public bool testConnection()
{
string strFTPServer = string.Empty;
string strUserName = string.Empty;
string strPassword = string.Empty;
string testconnection = string.Empty;
WebRequest request = WebRequest.Create("ftp://" + strFTPServer);
request.Method = WebRequestMethods.Ftp. ListDirectoryDetails;
request.Credentials = new NetworkCredential(strUserName, strPassword);
try
{
using (var resp = (FtpWebResponse)request. GetResponse())
{
//MessageBox.Show(resp. Welcome);
return true;
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
return false;
}
private void MoveAllFilesFromSourceToFTP( string strSourcedirectory,int strAllSourceFilesCount)
{
if (Directory.Exists( strSourcedirectory))
{
string strftpdestinationFileCopy = string.Empty;
string strFolderPath = string.Empty;
int ftpAllFilesCount = 0;
RegistryKey FolderKey = Registry.CurrentUser. OpenSubKey(@" DestinationFolder\WinRegistry" );
if (FolderKey != null)
{
strFolderPath = FolderKey.GetValue(" FolderPath").ToString();
}
DirectoryInfo dir = new DirectoryInfo(@"" + strSourcedirectory + "");
FileInfo[] filescount = dir.GetFiles();
if (filescount.Length != 0)
{
string strFTPServer = string.Empty;
string strUserName = string.Empty;
string strPassword = string.Empty;
// Copy the files and overwrite destination files if they already exist.
RegistryKey key = Registry.CurrentUser. OpenSubKey(@"MoveFiles\ WinRegistry");
if (key != null)
{
strFTPServer = key.GetValue("FTPServer"). ToString();
strUserName = key.GetValue("UserName"). ToString();
strPassword = key.GetValue("Password"). ToString();
}
foreach (FileInfo file in dir.GetFiles())
{
//Here get file upload status of FTP
bool fileuploadStatus = Upload(file.FullName, dir.Name, strFTPServer, strUserName, strPassword);
{
Response.Write("Transfer Files Completed.");
}
}
}}
private bool Upload(string fullpathwithfilename, string CurrentDate, string ftpServerIP, string ftpUserID, string ftpPassword)
{
FileInfo fileInf = new FileInfo(fullpathwithfilename) ;
string ftpfullpath = "ftp://" + ftpServerIP + "/" + CurrentDate + "/";
bool status = DirectoryExists(ftpfullpath, ftpUserID, ftpPassword);
if (status != true)
{
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest. Create(ftpfullpath);
ftp.Method = WebRequestMethods.Ftp. MakeDirectory;
ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse CreateForderResponse = (FtpWebResponse)ftp. GetResponse();
}
string ftpUploadFullPath = "ftp://" + ftpServerIP + "/" + CurrentDate + "/" + fileInf.Name + "";
FtpWebRequest ftpUpLoadFile = (FtpWebRequest)FtpWebRequest. Create(ftpUploadFullPath);
ftpUpLoadFile.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftpUpLoadFile.KeepAlive = false;
ftpUpLoadFile.UseBinary = true;
ftpUpLoadFile.Method = WebRequestMethods.Ftp. UploadFile;
FileStream fs = File.OpenRead( fullpathwithfilename);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
Stream ftpstream = ftpUpLoadFile. GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
return true;
}
Comments
Post a Comment