/// <summary> /// 智能获取SectionName /// </summary> /// <param name="objId"></param> /// <returns></returns> private static string[] SmartSection(IniDocumentHelper objId) { List<string> arKeys = null; //string sKey = string.Empty; string oCount = objId[STypeNode, "Count"]; string sType = objId[STypeNode, "RemoteType"]; if (objId.SectionNames.Contains(sType, StringComparer.OrdinalIgnoreCase)) { if (sType != null) { if (arKeys == null) arKeys = new List<string>(); arKeys.Add(sType); } } else { if (oCount == null) return arKeys.ToArray(); int iCount = Convert.ToInt32(oCount); for (int i = 1; i < iCount + 1; i++) { string sKey = sType + i; if (!objId.SectionNames.Contains(sKey, StringComparer.OrdinalIgnoreCase)) continue; if (arKeys == null) arKeys = new List<string>(); arKeys.Add(sKey); } } return arKeys.ToArray(); }
private static Dictionary<string, object> ReadConfigFile(string aFull, Dictionary<string, object> arConfigs, string cv) { bool bIsXml = XmlHelper.CheckIsXml(aFull); if (bIsXml) { try { var objFi = new FileInfo(aFull); string dBase = objFi.DirectoryName; var document = new XmlDocument(); document.Load(aFull); XmlNode objR = document.SelectSingleNode(XmlHelper.RecPath); string s1 = objR.Attributes["value"].Value; XmlNode objS = document.SelectSingleNode(XmlHelper.SenPath); string s2 = objS.Attributes["value"].Value; if (s1 == "tcp_client") { string nesPath = string.Format("transfer{0}win32_socket", Path.DirectorySeparatorChar); string rConfig = string.Format("{0}{1}{2}{1}win32_socket_pto.cfg", dBase, Path.DirectorySeparatorChar, nesPath); var objXr = new XmlDocument(); objXr.Load(rConfig); XmlNode objXri = objXr.SelectSingleNode(XmlHelper.TcpAddressPath); string sIp = objXri.Attributes["value"].Value; XmlNode objXrp = objXr.SelectSingleNode(XmlHelper.TcpPortPath); string sPort = objXrp.Attributes["value"].Value; var arRec = new Dictionary<string, string> {{"Address", sIp}, {"Port", sPort}}; if (arConfigs == null) arConfigs = new Dictionary<string, object>(); arConfigs.Add("win32_socket_pto.cfg", arRec); } string dstPath = string.Empty; var arS2 = s2.Split(','); foreach (var as2 in arS2) { dstPath = string.Format("{0}{1}Signal{1}{2}", dBase, Path.DirectorySeparatorChar, as2); var dstDi = new DirectoryInfo(dstPath); FileInfo[] arDlls = dstDi.GetFiles("*.dll"); if (arDlls != null && arDlls.Any()) { FileInfo aDll = arDlls[0]; string dllName = aDll.FullName; string dllCfgName = dllName.Replace(".dll", ".cfg"); var objXs = new XmlDocument(); objXs.Load(dllCfgName); XmlNode objXsi = objXs.SelectSingleNode(XmlHelper.SenServerNamePath); string sIp = objXsi.Attributes["value"].Value; XmlNode objXsp = objXs.SelectSingleNode(XmlHelper.SenPortPath); string sPort = objXsp.Attributes["value"].Value; XmlNode objDdt = objXs.SelectSingleNode(XmlHelper.SenDataDestType); string sDestType = objDdt.Attributes["value"].Value; XmlNode objDir = objXs.SelectSingleNode(XmlHelper.SenDirectory); string sDir = objDir.Attributes["value"].Value; var arRec = new Dictionary<string, string> { { "DestType", sDestType }, { "ServerName", sIp }, { "Port", sPort }, { "Directory", sDir } }; if (arConfigs == null) arConfigs = new Dictionary<string, object>(); arConfigs.Add(aDll.Name, arRec); } } } catch (Exception ex) { } } else { var objId = new IniDocumentHelper(aFull, Encoding.GetEncoding("GB2312")); string[] obj = objId.SectionNames; if (obj.Length <= 0 || !obj.Contains(STypeNode, StringComparer.OrdinalIgnoreCase)) return arConfigs; string[] arKeys = SmartSection(objId); string sVirt; Dictionary<int, Dictionary<string, string>> sVals = ReadProperties(objId, arKeys, out sVirt); if (arConfigs == null) arConfigs = new Dictionary<string, object>(); if (!arConfigs.ContainsKey(cv)) arConfigs.Add(cv, JsonConvert.SerializeObject(sVals)); if (!string.IsNullOrEmpty(sVirt)) arConfigs.Add("Virt_" + cv, sVirt); } return arConfigs; }
/// <summary> /// 读取Section属性 /// </summary> /// <param name="objId"></param> /// <param name="sKey"></param> /// <param name="sVirt">形成的虚拟串</param> /// <returns></returns> private static Dictionary<int, Dictionary<string, string>> ReadProperties(IniDocumentHelper objId, string[] arKeys, out string sVirt) { Dictionary<int, Dictionary<string, string>> sVals = null; sVirt = string.Empty; List<string> arOut = null; for (int i = 0; i < arKeys.Length; i++) { string sKey = arKeys[i]; NameValueCollection z = objId[sKey]; List<string> arVirt = null; if (sVals == null) sVals = new Dictionary<int, Dictionary<string, string>>(); Dictionary<string, string> arDicPair = null; foreach (string tmz in z.Keys) { if (arDicPair == null) arDicPair = new Dictionary<string, string>(); if (!ArSkipProp.Contains(tmz)) arDicPair[tmz] = z[tmz]; //set values here if (!ArJoinProp.Contains(tmz)) continue; if (arVirt == null) arVirt = new List<string>(); arVirt.Add(z[tmz]); } sVals.Add(i, arDicPair); if (arVirt != null && arVirt.Count > 0) { string aVirt = string.Join(":", arVirt.ToArray()); if (arOut == null) arOut = new List<string>(); arOut.Add(aVirt); } } if (arOut != null) sVirt = string.Join(";", arOut.ToArray()); return sVals; }
private static Dictionary<string, object> GetConfigByKeyPair(string sShortName, FileSystemInfo di) { Dictionary<string, object> arConfigs = null; foreach ( Dictionary<string, string>.ValueCollection objTarConfigs in from ck in DicConfigs.Keys where ck == sShortName || sShortName.StartsWith(ck) select DicConfigs[ck].Values) { foreach (string otc in objTarConfigs) { //Build filename string aFull = string.Format("{0}{1}{2}", di.FullName, Path.DirectorySeparatorChar, otc); if (!File.Exists(aFull)) continue; var objId = new IniDocumentHelper(aFull, Encoding.GetEncoding("GB2312")); string[] obj = objId.SectionNames; if (obj.Length <= 0 || !obj.Contains(STypeNode, StringComparer.OrdinalIgnoreCase)) continue; string[] arKeys = SmartSection(objId); string sVirt; Dictionary<int, Dictionary<string, string>> sVals = ReadProperties(objId, arKeys, out sVirt); if (arConfigs == null) arConfigs = new Dictionary<string, object>(); arConfigs.Add(otc, string.Format("{0}/{1}:{2}", otc, arKeys, JsonConvert.SerializeObject(sVals))); if (!string.IsNullOrEmpty(sVirt)) arConfigs.Add(string.Format("Virt_{0}", otc), sVirt); } break; } return arConfigs; }