using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Euro.Transfer.Base
{
///
/// 工具类
///
public class ServiceTools : ServiceBase, IConfigurationSectionHandler
{
///
/// 获取configSections节点
///
///
public static XmlNode GetConfigSections()
{
var doc = new XmlDocument();
doc.Load(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).FilePath);
return doc.DocumentElement.FirstChild;
}
///
/// 获取section节点
///
///
///
public static NameValueCollection GetSection(string nodeName)
{
return (NameValueCollection)ConfigurationManager.GetSection(nodeName);
}
///
/// 停止Windows服务
///
/// 服务名称
public static void WindowsServiceStop(string serviceName)
{
var control = new System.ServiceProcess.ServiceController(serviceName);
control.Stop();
control.Dispose();
}
///
/// 写日志
///
/// 日志文件
/// 日志内容
/// 是否追加方式
/// todo: describe foder parameter on WriteWordLog
public static void WriteWordLog(string path, string cont, bool isAppend, string foder)
{
if (path == "")
{
path = System.Windows.Forms.Application.StartupPath.ToString() + @"\WordLogs\" + foder + "\\";
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
using (StreamWriter sw = new StreamWriter(path, isAppend, Encoding.UTF8))
{
sw.WriteLine(DateTime.Now);
sw.WriteLine(cont);
sw.WriteLine("");
sw.Close();
}
}
///
/// 写錯誤日志
///
/// 日志文件
/// 日志内容
/// 是否追加方式
public static void WriteLog(string path, string cont, bool isAppend)
{
if (path == "")
{
path = System.Windows.Forms.Application.StartupPath.ToString() + @"\ErrorLogs\";
}
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path += "//" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
using (StreamWriter sw = new StreamWriter(path, isAppend, Encoding.UTF8))
{
sw.WriteLine(DateTime.Now);
sw.WriteLine(cont);
sw.WriteLine("");
sw.Close();
}
}
///
/// 写文字檔
///
/// 文字檔内容
/// 文字檔副檔名
/// 文字檔前最
/// todo: describe orgid parameter on WriteWords
public static void WriteWords(string cont, string orgid, string subname, string prename = "")
{
var path = Common.ConfigGetValue("", "WriteWordPath") + "\\";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
var sTransferID = Common.ConfigGetValue("", "TransferID");
var name = Common.GetSystemSetting(orgid, sTransferID);
path += prename + name + subname;
using (StreamWriter sw = new StreamWriter(path, true, Encoding.Default))
{
sw.WriteLine(cont);
sw.Close();
}
}
///
/// 实现接口以读写app.config
///
///
///
///
///
public object Create(object parent, object configContext, XmlNode section)
{
var handler = new NameValueSectionHandler();
return handler.Create(parent, configContext, section);
}
}
}