public void CreateConfig() { try { XDocument x_config = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); x_config.Add( new XElement(s_root, new XElement(s_action, new XAttribute(a_mode, "monitor"), new XAttribute(a_interval, "1000"), new XAttribute(a_process, "4")), new XElement(s_database, new XAttribute(a_ip, "127.0.0.1"), new XAttribute(a_usr, "root"), new XAttribute(a_pwd, "pwd"), new XAttribute(a_db, "db_specimen")), new XElement(s_dir, new XAttribute(a_source, "D:\\"), new XAttribute(a_dest, "D:\\"), new XAttribute(a_cache, "500"), new XAttribute(a_sharepath, "您的共享路径")), new XElement(s_type, new XElement(s_match, new XAttribute(a_ext, ".doc"), new XAttribute(a_ksotype, "wps")) ) ) ); x_config.Save(mConfigPath); mlog.Log_i("This is first time you run this application. We just made a config file for you. Please modify it and restart this application again."); Console.ReadKey(true); } catch (Exception e) { AppLogHelper.Log_i(String.Format("{0, 76}", new String('-', 36))); mlog.Log_e("Create Config File Failed: " + mConfigPath + " -> " + e.Message); } }
public bool LoadConfig(out bool exitApplication) { exitApplication = false; try { if (!File.Exists(mConfigPath)) { CreateConfig(); exitApplication = true; return(true); } XDocument x_config = XDocument.Load(mConfigPath); XElement x_root = x_config.Element(s_root); mMode = x_root.Element(s_action).Attribute(a_mode).Value; mInterval = int.Parse(x_root.Element(s_action).Attribute(a_interval).Value); mProcess = int.Parse(x_root.Element(s_action).Attribute(a_process).Value); mRunningMode = mMode == "monitor" ? Mode.Monitor : Mode.Once; mIp = x_root.Element(s_database).Attribute(a_ip).Value; mUsr = x_root.Element(s_database).Attribute(a_usr).Value; mPwd = x_root.Element(s_database).Attribute(a_pwd).Value; mDatabase = x_root.Element(s_database).Attribute(a_db).Value; mDir_src = x_root.Element(s_dir).Attribute(a_source).Value; mDir_dest = x_root.Element(s_dir).Attribute(a_dest).Value; mSharePath = x_root.Element(s_dir).Attribute(a_sharepath).Value; mCache = int.Parse(x_root.Element(s_dir).Attribute(a_cache).Value); foreach (var match in x_root.Element(s_type).Elements(s_match)) { if (!mDicType.ContainsKey(match.Attribute(a_ext).Value)) { mDicType.Add(match.Attribute(a_ext).Value.ToLower(), match.Attribute(a_ksotype).Value); } } mConnection = new MySqlDBConnection(mIp, mDatabase, mUsr, mPwd, AppLogHelper.Log); if (!mConnection.TestOpenClose()) { return(false); } return(true); } catch (Exception e) { AppLogHelper.Log_i(String.Format("{0, 76}", new String('-', 36))); mlog.Log_e("Parse Config File Failed: " + mConfigPath + " -> " + e.Message); return(false); } }
public void ParseDirectory() { FileInfo[] files = null; try { if (!Directory.Exists(mConfig.DirectorySource)) { Directory.CreateDirectory(mConfig.DirectorySource); } files = new DirectoryInfo(mConfig.DirectorySource).GetFiles("*.*", SearchOption.AllDirectories); } catch (Exception e) { AppLogHelper.Log_i(String.Format("{0, 76}", new String('-', 36))); mLog.Log_e("Parse Directory Failed: (" + mConfig.DirectorySource + ")" + e.Message); } try{ int threadNum = 0; List <String> Md5List = new List <String>(); foreach (var file in files) { while (threadNum >= mConfig.Process) { Thread.Sleep(100); } Thread t = new Thread(new ParameterizedThreadStart(( object fullname) => { try { FileOperate(fullname.ToString(), Md5List); } catch { } finally { threadNum--; } })); threadNum++; t.Start(file.FullName); } } catch (Exception e) { AppLogHelper.Log_i(String.Format("{0, 76}", new String('-', 36))); mLog.Log_e("Parse Directory Failed: " + e.Message); } }
/// <summary> /// 对文件进行操作,按照规则放进相应的文件夹 /// </summary> private void FileOperate(String filename, List <String> Md5List, bool loop = false) { try { if (Path.GetFileName(filename).StartsWith("$__")) { return; } Int32 folderID; String date = DateTime.Today.ToString("yyyyMMdd"); String ext = Path.GetExtension(filename).ToLower(); if (!mConfig.DictionaryKsotype.ContainsKey(ext)) { return; } if (ext.Trim() == "") { ext = "-"; } String ksotype = GetKsoType(ext); String path = Path.Combine(mConfig.DirectoryDest, date, ksotype, ext); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } int folderCount = Directory.GetDirectories(path).Length; //Folder start with 1 String finalPath = Path.Combine(path, "1"); if (folderCount == 0) { Directory.CreateDirectory(finalPath); } else { folderID = folderCount; finalPath = Path.Combine(path, folderID.ToString()); if (Directory.GetFiles(finalPath).Length >= mConfig.Cache) { folderID++; finalPath = Path.Combine(path, folderID.ToString()); Directory.CreateDirectory(finalPath); } } //思路:如果前面是MD5码,则去掉 String fileMd5 = GetFileMD5(filename); String xml_filename = ""; String originalFilename = Path.GetFileNameWithoutExtension(filename); String finalName; if (originalFilename.Length > 33 && originalFilename.Substring(0, 33) == fileMd5 + "_") { finalName = originalFilename.Substring(33); } else { finalName = originalFilename; } xml_filename = Path.Combine(Path.GetDirectoryName(filename), "$__" + Path.GetFileName(finalName)) + Path.GetExtension(filename) + ".xml"; if (finalName.Length > 20) { finalName = finalName.Substring(0, 20); } if (!IsDuplicateByMd5(fileMd5) && !Md5List.Contains(fileMd5)) { Md5List.Add(fileMd5); if (Md5List.Count > 511) { lock (this) { try { Md5List.RemoveAt(0); //mLog.Log_i("移除MD5列表底层项。"); } catch { } } } int num = 0; // 汉字的正则表达式匹配 [\\u4e00-\\u9fa5] Regex legalChar = new Regex("[A-Za-z0-9]|[\\u4e00-\\u9fa5]"); StringBuilder _sb = new StringBuilder(); foreach (char c in finalName) { if (legalChar.IsMatch(c.ToString())) { _sb.Append(c); } } finalName = Path.Combine(finalPath, _sb.ToString() + Path.GetExtension(filename)); string _finalName; if (File.Exists(finalName)) { do { num++; _finalName = Path.Combine(finalPath, Path.GetFileNameWithoutExtension(finalName) + "_" + num + Path.GetExtension(finalName)); } while (File.Exists(_finalName)); finalName = _finalName; } String ip = null; String host = null; Match matchIpHost = RegIpHost.Match(filename); if (matchIpHost.Success) { ip = matchIpHost.Result("${ip}"); host = matchIpHost.Result("${host}"); } ip = (ip == null || ip.Trim() == "") ? IP_ADDRESS : ip; host = (host == null || host.Trim() == "") ? HOST_NAME : host; String link = "", keyword = "", language = ""; try { XDocument xdoc = XDocument.Load(xml_filename); XElement xroot = xdoc.Element("root"); link = xroot.Element("url").Value; keyword = xroot.Element("keyword").Value; if (xroot.Element("search_language") != null) { language = xroot.Element("search_language").Value; } } catch { } try { string replacePath = mConfig.SharePath; if (finalName.EndsWith(@"\")) { if (!replacePath.EndsWith(@"\")) { replacePath += @"\"; } } string sharepath = finalName.Replace(mConfig.DirectoryDest, replacePath); InsertInformation(ext, sharepath, fileMd5, ip, host, link, keyword, language); File.Delete(xml_filename); } catch (Exception ex) { Log.Log_e(ex.Message); } //如果是监视模式,则剪切 if (mConfig.RunningMode == Configuration.Mode.Once) { File.Copy(filename, finalName, true); AppLogHelper.Log_o(" File Copied And Inserted -> " + filename + " -> " + finalName); } else { File.Move(filename, finalName); AppLogHelper.Log_o(" File Moved And Inserted -> " + filename + " -> " + finalName); } } else { if (mConfig.RunningMode == Configuration.Mode.Monitor) { File.Delete(filename); File.Delete(xml_filename); AppLogHelper.Log_w(" Duplicated File And Deleted-> " + filename); } else { AppLogHelper.Log_w(" Duplicated File -> " + filename); } } } catch (InvalidOperationException) { //再试一次 if (!loop) { FileOperate(filename, Md5List, true); } } catch (Exception e) { mLog.Log_e("Operation Failed: " + e.Message); } }