using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Xml;
namespace G3ERP.Win.MedicineProtectInterface
{
public class Helper
{
[DllImport("kernel32")] // 读取配置文件的接口
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size, string filePath);
//保存至临时文件
public static void xmlSetValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();
string strExecutablePath = AppDomain.CurrentDomain.BaseDirectory + "FileInfo";//保存在\bin\Debug中的profileInfo.xml中
// 获取可执行文件的路径和名称
xDoc.Load(strExecutablePath + ".xml");
XmlNode xNode;
XmlElement xElem1;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");
if (xElem1 != null) xElem1.SetAttribute("value", AppValue);
xDoc.Save(strExecutablePath + ".xml");
}
/// <summary>
/// 读操作
/// </summary>
/// <param name="strExecutablePath"></param>
/// <param name="appKey"></param>
/// <returns></returns>
public static string xmlGetValue(string appKey)
{
string strExecutablePath = AppDomain.CurrentDomain.BaseDirectory + "FileInfo";
XmlDocument xDoc = new XmlDocument();
try
{
xDoc.Load(strExecutablePath + ".xml");
XmlNode xNode;
XmlElement xElem;
xNode = xDoc.SelectSingleNode("//appSettings");
xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
if (xElem != null)
return xElem.GetAttribute("value");
else
return "";
}
catch (Exception ex)
{
return "";
}
}
/// <summary>
/// 记录log日志
/// </summary>
/// <param name="text"></param>
public static void WriteLog(string text)
{
object obj = new object();
string filePath = AppDomain.CurrentDomain.BaseDirectory + "Log/Error.log";
lock (obj)
{
using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
{
sw.WriteLine(DateTime.Now.ToString() + "\t" + text);
sw.Flush();
}
}
}
// 读取配置文件的值
public static string ProfileReadValue(string section, string key)
{
//配置文件位置
string path = AppDomain.CurrentDomain.BaseDirectory + "license.ini";
StringBuilder sb = new StringBuilder(255);
GetPrivateProfileString(section, key, "", sb, 255, path);
return sb.ToString().Trim();
}
}
}