/// <summary> /// 检查本地是否有未索引的文件 /// </summary> public void Check() { string localdir = ConfigValue; //获得本地的文件夹们 DirectoryInfo theFolder = new DirectoryInfo(localdir + Host + @"\"); if (theFolder.Exists == false) { return; } log.Info("Ftp文件检查:" + Host + @" " + User); foreach (FileInfo NextFile in theFolder.GetFiles()) { //将文件加入检查队列 QFtpChk.Enqueue(NextFile.FullName); } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { //空文件夹删除 if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空Ftp文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectory(NextFolder.FullName); } } flag = false; }
/// <summary> /// 检查本地文件夹 /// </summary> /// <param name="strdir">文件夹路径</param> private void CheckDirectory(string strdir) { DirectoryInfo theFolder = new DirectoryInfo(strdir); //遍历文件 foreach (FileInfo NextFile in theFolder.GetFiles()) { //将文件加入检查队列 QFtpChk.Enqueue(NextFile.FullName); } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { //若是空文件夹,删除 log.Info("删除空Ftp文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectory(NextFolder.FullName); //递归检查 } } }
/// <summary> /// 检查本地是否有未索引的 /// </summary> public void Check() { string localdir = ConfigValue; //获得本地的文件夹们 DirectoryInfo theFolder = new DirectoryInfo(localdir); if (theFolder.Exists == false) { return; } log.Info("开始检查共享文件夹同步"); foreach (FileInfo NextFile in theFolder.GetFiles()) { //加入检查队列 QShareChk.Enqueue(NextFile.FullName); } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { //空文件夹删之 if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空share文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectoryforreg(NextFolder.FullName); //调用文件夹删除检查 } } flag = false; }
/// <summary> /// 递归检查本地文件夹 /// </summary> /// <param name="strdir">文件夹路径</param> private void CheckDirectoryforreg(string strdir) { DirectoryInfo theFolder = new DirectoryInfo(strdir); //遍历文件 foreach (FileInfo NextFile in theFolder.GetFiles()) { //加入检查队列 QShareChk.Enqueue(NextFile.FullName); } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { //空文件夹删之 if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空share文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectoryforreg(NextFolder.FullName); //递归检查 } } }
private void getDir(DirectoryInfo dir, ref int dircount, ref int count) { dircount += dir.GetDirectories().Count(); foreach (DirectoryInfo NextFolder in dir.GetDirectories()) { count += getFileCount(NextFolder); if (NextFolder.GetDirectories().Count() > 0) { getDir(NextFolder, ref dircount, ref count); } } }
/// <summary> /// 检查文件夹是否含有要被删的 /// </summary> /// <param name="strdir">文件夹本地路径</param> /// <param name="session">远程Session</param> /// <param name="Host">Ftp地址</param> private void CheckDirectory(string strdir, Session session, string Host) { DirectoryInfo theFolder = new DirectoryInfo(strdir); string localdir = ConfigValue; //获得本地的文件夹们 //遍历文件 foreach (FileInfo NextFile in theFolder.GetFiles()) { string name, remote, finalstr; string filexten = Path.GetExtension(NextFile.Name); //扩展名检查 if (filexten == ".rntf") { name = NextFile.FullName; remote = name.Replace(localdir + Host, ""); finalstr = remote.Replace(@"\", @"/"); finalstr = finalstr.Replace(".rntf", ""); } else { name = NextFile.FullName; remote = name.Replace(localdir + Host, ""); finalstr = remote.Replace(@"\", @"/"); } //统一文件名和路径名,到远程服务器检测是否存在 if (session.FileExists(finalstr)) { continue; //存在就不用管 } else { //不存在删除本地文件 File.Delete(name); log.Info("删除Ftp文件" + name); QFtpDel.Enqueue(name); } } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { //若文件夹已经是空文件夹,删除该文件夹 if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空Ftp文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectory(NextFolder.FullName, session, Host); //递归检查 } } }
/// <summary> /// 检查是否存在 /// </summary> /// <param name="Host">Ftp地址</param> /// <param name="User">用户名</param> /// <param name="Psw">密码</param> public void CheckExist() { try { // 创建session SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = Host, UserName = User, Password = Psw, }; using (Session session = new Session()) { session.Open(sessionOptions); string localdir = ConfigValue; //获得本地的文件夹们 DirectoryInfo theFolder = new DirectoryInfo(localdir + Host + @"\"); log.Info("Ftp建立连接:" + Host + @" " + User); //获取文件夹内的文件 foreach (FileInfo NextFile in theFolder.GetFiles()) { string name, remote, finalstr; string filexten = Path.GetExtension(NextFile.Name); //检查扩展名 if (filexten == ".rntf") { name = NextFile.FullName; remote = name.Replace(localdir + Host, ""); finalstr = remote.Replace(@"\", @"/"); finalstr = finalstr.Replace(".rntf", ""); } else { name = NextFile.FullName; remote = name.Replace(localdir + Host, ""); finalstr = remote.Replace(@"\", @"/"); } //在统一链接之后检查远程文件是否存在 if (session.FileExists(finalstr)) { continue; } else { //不存在则删除 QFtpDel.Enqueue(name); File.Delete(name); log.Info("删除Ftp文件" + name); } } //遍历文件夹 foreach (DirectoryInfo NextFolder in theFolder.GetDirectories()) { if (NextFolder.GetDirectories().Length == 0 && NextFolder.GetFiles().Length == 0) { log.Info("删除空Ftp文件夹" + NextFolder.FullName); Directory.Delete(NextFolder.FullName); continue; } else { CheckDirectory(NextFolder.FullName, session, Host); } } flag = false; } } catch (Exception e) { log.Error("Ftp错误:" + e.Message); Console.WriteLine("Error: {0}", e); flag = false; } }