Files
lserp_cs_5.0/Lskj.Test/LSServerService/LSIniFile.cs
T
2026-07-10 15:25:05 +08:00

144 lines
4.6 KiB
C#

namespace LSServerService
{
using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
public static class LSIniFile
{
private static string _IniPath = "";
private static XmlDocument xmlDoc = new XmlDocument();
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
public static XmlNodeList ReadNodeList(string strNodeName)
{
XmlNodeList elementsByTagName;
try
{
elementsByTagName = xmlDoc.GetElementsByTagName(strNodeName);
}
catch (Exception exception)
{
PubUtils.WriteLog("error: {0}", new object[] { exception });
return null;
}
return elementsByTagName;
}
public static string ReadNodeValue(XmlNode node, string strName)
{
string str = null;
if (node.HasChildNodes)
{
for (int i = 0; i < node.ChildNodes.Count; i++)
{
if (node.ChildNodes[i].LocalName == strName)
{
return node.ChildNodes[i].InnerXml;
}
}
return str;
}
return null;
}
public static XmlNode ReadSingleNode(string strNodeName)
{
XmlNode node = null;
try
{
node = xmlDoc.DocumentElement.SelectSingleNode(strNodeName);
}
catch (Exception exception)
{
PubUtils.WriteLog("error: {0}", new object[] { exception });
return null;
}
return node;
}
public static string ReadValue(string NodePath, string NodeName)
{
try
{
for (XmlNode node = xmlDoc.DocumentElement.SelectNodes(NodePath)[0].FirstChild; node != null; node = node.NextSibling)
{
if (node.Name == NodeName)
{
return node.InnerText;
}
}
}
catch (Exception exception)
{
PubUtils.WriteLog("error: {0}", new object[] { exception });
}
return "";
}
public static bool ReadValue(string NodeName, int index, params string[] NodeValues)
{
try
{
XmlNode node = ReadSingleNode(NodeName);
if (node.ChildNodes.Count <= index)
{
return false;
}
node = node.ChildNodes[index];
int num = 0;
for (XmlNode node2 = node.FirstChild; (node2 != null) && (num < NodeValues.Length); node2 = node2.NextSibling)
{
if (node2.NodeType == XmlNodeType.Element)
{
NodeValues[num] = node2.InnerText;
num++;
}
}
return true;
}
catch (Exception exception)
{
PubUtils.WriteLog("error: {0}", new object[] { exception });
return false;
}
}
public static string ReadValue(string Section, string Key, string path)
{
StringBuilder retVal = new StringBuilder(255);
int num = GetPrivateProfileString(Section, Key, "", retVal, 255, path);
return retVal.ToString();
}
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
public static void WriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, _IniPath);
}
public static void WriteValue(string Section, string Key, string Value, string path)
{
WritePrivateProfileString(Section, Key, Value, path);
}
public static string IniPath
{
set
{
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
if (!baseDirectory.EndsWith(@"\"))
{
baseDirectory = baseDirectory + @"\";
}
_IniPath = baseDirectory + value;
xmlDoc.Load(_IniPath);
}
}
}
}