/// <summary> /// 切换到下级目录,用\\进行目录分割,例如CNC\\FCYA1 /// </summary> /// <param name="sFolderName"></param> public void NextDirectory(string sFolderName) { try { ftpConnection.SetCurrentDirectory(Combine(ftpConnection.GetCurrentDirectory(), sFolderName)); } catch { throw new Exception(string.Format("没有发现对应下级目录{0}, 切换失败!", sFolderName)); } }
/// <summary> /// 下载 /// </summary> /// <param name="filePath"></param> /// <param name="fileName"></param> public void Download(string filePath, string fileName) { try { conn.GetFile(conn.GetCurrentDirectory() + "/" + fileName, filePath + "\\" + fileName, false); } catch { MessageBox.Show("ftp has lost connection!", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void SendAddOnToServer(List <string> files, String folderPath) { Console.WriteLine("Sending to server"); TextReader tr = new StreamReader(@"C:\Users\Pilus\Documents\Cloud Backup\PrologueServer.txt"); string addr = tr.ReadLine(); string user = tr.ReadLine(); string pass = tr.ReadLine(); using (FtpConnection ftp = new FtpConnection(addr, user, pass)) { ftp.Open(); /* Open the FTP connection */ ftp.Login(); /* Login using previously provided credentials */ int c = 0; int c2 = 0; int transfered = 0; foreach (String file in files) { try { // Check if the directory exists String relativeFilePath = file.Replace(folderPath, "").Replace(@"\", "/"); String totalDirPath = relativeFilePath.Substring(0, relativeFilePath.LastIndexOf("/")); String folder = totalDirPath.Substring(totalDirPath.LastIndexOf("/") + 1); String topDir = "/" + totalDirPath.Replace("/" + folder, ""); if (!ftp.GetCurrentDirectory().Equals("/" + totalDirPath)) { ftp.SetCurrentDirectory(topDir); if (!ftp.DirectoryExists(folder)) { ftp.CreateDirectory(folder); } } if (PutFile(ftp, file) == true) { transfered++; } c++; int c3 = (c / (files.Count / 20)); if (c3 > c2) { Console.Write("{0}% ", c3 * 5); c2 = c3; } } catch (FtpException e) { Console.WriteLine(String.Format("FTP Error: {0} {1}", e.ErrorCode, e.Message)); } } Console.WriteLine("{0} files created/updated.", transfered); } }
private static void DownloadFiles(ServerInfo server) { string tempFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + "MapUpdater"; ConnectionInfo connection = server.MinecraftConnection; if (connection.Type == ConnectionType.Ftp) { using (FtpConnection ftp = new FtpConnection(connection.Address, 21, connection.Username, connection.Password)) { ftp.Open(); ftp.Login(); ftp.SetCurrentDirectory(connection.WorldsFolder); if (ftp.GetCurrentDirectory() != connection.WorldsFolder) { Console.WriteLine("WorldsFolder does not exist on server"); } DownloadDirectory(ftp, server.World.Name, tempFolder + Path.DirectorySeparatorChar + server.World.Name); } } }
public void ClearFtp(FtpConnection ftp, string directory) { ftp.SetCurrentDirectory(directory); var dirs = ftp.GetDirectories(); foreach (var dir in dirs) { if ((dir.Name != ".") && (dir.Name != "..")) { ClearFtp(ftp, dir.Name); //Recursive call ftp.RemoveDirectory(dir.Name); } } foreach (var file in ftp.GetFiles()) { ftp.RemoveFile(file.Name); } if (ftp.GetCurrentDirectory() != "/") { ftp.SetCurrentDirectory(".."); } }
private static void UploadMapImages(ServerInfo server) { string currentDirectory = Environment.CurrentDirectory; ConnectionInfo connection = server.WebConnection; if (connection.Type == ConnectionType.Ftp) { using (FtpConnection ftp = new FtpConnection(connection.Address, 21, connection.Username, connection.Password)) { ftp.Open(); ftp.Login(); ftp.SetCurrentDirectory(connection.ImagesFolder); if (ftp.GetCurrentDirectory() != connection.ImagesFolder) { Console.WriteLine("ImagesFolder does not exist on server"); } foreach (string file in Directory.GetFiles(Environment.CurrentDirectory, "*.png")) { ftp.PutFile(file); } } } }
private void BtnGetCurrentDirectoryClick(object sender, EventArgs e) { tbCurrentDirectory.Text = _ftp.GetCurrentDirectory(); }