替换日志,日志写入文件
This commit is contained in:
54
精工涂胶检测项目/CVE.LogViewer/Helper.cs
Normal file
54
精工涂胶检测项目/CVE.LogViewer/Helper.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Xml;
|
||||
|
||||
namespace CVE.LogViewer
|
||||
{
|
||||
public class Helper
|
||||
{
|
||||
private const string FileName = "LogConfig.xml";
|
||||
public Stream GetXMLStream(string ConfigDir)
|
||||
{
|
||||
if (!System.IO.Directory.Exists(ConfigDir))
|
||||
{
|
||||
System.IO.Directory.CreateDirectory(ConfigDir);//不存在就创建目录
|
||||
}
|
||||
|
||||
string ConfigPath = ConfigDir + "\\" + FileName;
|
||||
//判断本地是否存在配置文件
|
||||
if (File.Exists(ConfigPath))
|
||||
{
|
||||
//导入本地配置文件
|
||||
Stream stream = new FileStream(ConfigPath, FileMode.Open, FileAccess.Read);
|
||||
return stream;
|
||||
}
|
||||
else
|
||||
{
|
||||
//载入程序集中配置文件
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var stream = assembly.GetManifestResourceStream(this.GetType(), FileName);
|
||||
try
|
||||
{
|
||||
if (stream == null)
|
||||
{
|
||||
throw new FileNotFoundException("Could not find embedded mappings resource file.", FileName);
|
||||
}
|
||||
else
|
||||
{
|
||||
//将默认配置文件保存至指定目录
|
||||
XmlDocument doc = new XmlDocument();
|
||||
doc.Load(stream);
|
||||
doc.Save(ConfigPath);
|
||||
}
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
return stream;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user