55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
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;
|
|
}
|
|
}
|
|
}
|
|
}
|