2个回答
展开全部
这个不是网上下的,是我自己底层库里的代码,都经过测试的,请放心使用 /// <summary> /// FTP类 /// 提供常用FTP上传下载等数据处理函数 /// </summary> public class FTP { private static Regex regexName; private string serverIP; private string userName; private string passWord; static FTP() { regexName = new Regex(@"[^\s]*$", RegexOptions.Compiled); } /// <summary> /// 初始化FTP类,赋予服务器IP地址,用户名和密码,以便所有操作自动登录。 /// </summary> /// <param name="serverIP">服务器IP地址</param> /// <param name="userName">用户名</param> /// <param name="passWord">密码</param> public FTP(string serverIP, string userName, string passWord) { this.serverIP = serverIP; this.userName = userName; this.passWord = passWord; } /// <summary> /// 判断FTP目录是否存在 /// </summary> /// <param name="directoryName">目录路径</param> /// <returns>是否存在</returns> public bool DirectoryExist(string directoryName) { return DirectoryExist(directoryName, serverIP, userName, passWord); } /// <summary> /// 在FTP上创建目录 /// </summary> /// <param name="directoryName">目录名</param> /// <returns>是否成功</returns> public bool CreateDirectory(string directoryName) { return CreateDirectory(directoryName, serverIP, userName, passWord); } /// <summary> /// 从FTP上删除文件 /// </summary> /// <param name="fileName">文件名</param> /// <returns>是否成功</returns> public bool DeleteFile(string fileName) { return DeleteFile(fileName, serverIP, userName, passWord); } /// <summary> /// 从FTP上删除目录(如果目录中含有文件,会引发异常) /// </summary> /// <param name="directoryName">目录名</param> /// <returns>是否成功</returns> public bool DeleteDirectory(string directoryName) { return DeleteDirectory(directoryName, serverIP, userName, passWord); } /// <summary> /// 判断文件是否存在于制定目录中 /// </summary> /// <param name="directoryName">目录名</param> /// <param name="ftpName">文件</param> /// <returns></returns> public bool FileExist(string directoryName,string fileName) { return FileExist(directoryName, fileName,serverIP,userName,passWord); } /// <summary> /// 判断目录是否为空(即没有文件) /// </summary> /// <param name="directoryName">目录名</param> /// <returns>是否为空</returns> public bool IsEmptyDirectory(string directoryName) { return IsEmptyDirectory(directoryName, serverIP, userName, passWord); } /// <summary> /// 判断目录是否有文件 /// </summary> /// <param name="directoryName">目录名</param> /// <returns>是否有文件</returns> public bool HasFileDirectory(string directoryName) { return HasFileDirectory(directoryName,serverIP,userName,passWord); } /// <summary> /// 从FTP下载文件,文件名称不变 /// </summary> /// <param name="sourcePath">源路径</param> /// <param name="targetPath">目标路径</param> /// <returns>是否成功</returns> public bool Download(string sourcePath, string targetPath) { return Download(sourcePath, targetPath, serverIP, userName, passWord); } /// <summary> /// 上传文件到FTP /// </summary> /// <param name="localFilePath">本地文件路径</param> /// <param name="targetDirectoryName"></param> /// <returns>是否成功</returns> public bool Upload(string localFilePath, string targetDirectoryName) { return Upload(localFilePath, targetDirectoryName, serverIP, userName, passWord); } }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询