/// <summary> /// 上传文件 /// </summary> public virtual void UpLoadFile(string sFileName, string remotePath) { try { FileInfo fileInf = new FileInfo(sFileName); var remoteFile = Combine(remotePath, fileInf.Name); if (ftpConnection.FileExists(remoteFile)) { Delete(remoteFile); } ftpConnection.PutFile(sFileName, remoteFile); } catch { throw; } }
private string AddendumForUniqueFilename(string fileName, FtpConnection ftp) { int i = 1; string uniqueFileName = fileName; while (ftp.FileExists(uniqueFileName)) { Logger.Debug("The file \"" + uniqueFileName + "\" already exist on ftp server directory"); i++; uniqueFileName = fileName + "_" + i; } if (i > 1) { return("_" + i); } return(""); }
public bool FTPTransferHHTToPC(string hostIP, bool checkpermissionMode) { using (FtpConnection ftp = new FtpConnection(hostIP, userFTP, passwordFTP)) { try { ftp.Open(); /* Open the FTP connection */ ftp.Login(userFTP, passwordFTP); /* Login using previously provided credentials */ string remoteFile = ""; string localFile = ""; if (checkpermissionMode) { remoteFile = HHTDBPath + validateDBName; localFile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\" + validateDBName; } else { remoteFile = HHTDBPath + DBName; localFile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\" + DBName; } if (ftp.FileExists(remoteFile)) /* check that a file exists */ { ftp.GetFile(remoteFile, localFile, false); /* download /incoming/file.txt as file.txt to current executing directory, overwrite if it exists */ return(true); } else { return(false); } } catch (Exception ex) { //Console.WriteLine(String.Format("FTP Error: {0} {1}", e.ErrorCode, e.Message)); log.Error(String.Format("Exception : {0}", ex.StackTrace)); return(false); } } }
public void Execute(IActivityRequest request, IActivityResponse response) { String sourcePath = request.Inputs["Source File Path"].AsString(); String savePath = request.Inputs["Destination File Path"].AsString(); bool overwriteLocal = Convert.ToBoolean(request.Inputs["Overwrite Local File"].AsString()); using (FtpConnection ftp = new FtpConnection(settings.FtpServer, settings.Port, settings.UserName, settings.Password)) { ftp.Open(); ftp.Login(); if (ftp.FileExists(sourcePath)) { ftp.GetFile(sourcePath, savePath, overwriteLocal); } else { response.LogErrorMessage("File does not exist at " + sourcePath); } } }
public static void getFile(ref string UserName, ref string PassWord, ref string ServerName, ref string FileName) { using (FtpConnection ftp = new FtpConnection("ServerName", "UserName", "PassWord")) { ftp.Open(); /* Open the FTP connection */ ftp.Login(); /* Login using previously provided credentials */ if (ftp.DirectoryExists("/incoming")) /* check that a directory exists */ { ftp.SetCurrentDirectory("/incoming"); /* change current directory */ } if (ftp.FileExists("/incoming/file.txt")) /* check that a file exists */ { ftp.GetFile("/incoming/file.txt", false); /* download /incoming/file.txt as file.txt to current executing directory, overwrite if it exists */ } //do some processing try { ftp.SetCurrentDirectory("/outgoing"); ftp.PutFile(@"c:\localfile.txt", "file.txt"); /* upload c:\localfile.txt to the current ftp directory as file.txt */ } catch (FtpException e) { Console.WriteLine(String.Format("FTP Error: {0} {1}", e.ErrorCode, e.Message)); } foreach (var dir in ftp.GetDirectories("/incoming/processed")) { Console.WriteLine(dir.Name); Console.WriteLine(dir.CreationTime); foreach (var file in dir.GetFiles()) { Console.WriteLine(file.Name); Console.WriteLine(file.LastAccessTime); } } } } // End getFile()
public bool FileExists(string filePath) { return(_ftpConnection.FileExists(filePath)); }