You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

184 lines
7.5 KiB

using PCScreenSavers.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace PCScreenSavers.Business
{
class Class_Config
{
private Class_Log log = new Class_Log();
private App app = ((App)System.Windows.Application.Current);
public void WriteToProgramConfig()
{
try
{
string fpath = Directory.GetCurrentDirectory() + "\\config\\";
if (!Directory.Exists(fpath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(fpath);
directoryInfo.Create();
}
string strConfigxml = AppDomain.CurrentDomain.BaseDirectory + "config//Program.xml";
if (File.Exists(strConfigxml) == true)
{
XmlDocument doc = new XmlDocument();
doc.Load(strConfigxml);
XmlNode xnFirst = doc.SelectSingleNode("Config");
XmlNode xnSecond = xnFirst.SelectSingleNode("ProgramInfo");
if (xnSecond == null)
{
log.WriteLogFile("配置文件错误-未发现ProgramInfo标签");
return;
}
else
{
XmlNode xnnode = xnSecond.SelectSingleNode("CurrentPlayIndex");
if (xnnode == null)
{
XmlElement Node = doc.CreateElement("CurrentPlayIndex");
Node.InnerText = app.currentIndex.ToString();
xnSecond.AppendChild(Node);
}
else
{
xnSecond.SelectSingleNode("CurrentPlayIndex").InnerText = app.currentIndex.ToString();
}
xnnode = xnSecond.SelectSingleNode("PlayTime");
if (xnnode == null)
{
XmlElement Node = doc.CreateElement("PlayTime");
Node.InnerText = app.PlayTime.ToString();
xnSecond.AppendChild(Node);
}
else
{
xnSecond.SelectSingleNode("PlayTime").InnerText = app.PlayTime.ToString();
}
}
doc.Save(strConfigxml);
}
else
{
XmlDocument doc = new XmlDocument(); // 创建dom对象
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0", "gb2312", null);
doc.AppendChild(xmldecl);
XmlElement root = doc.CreateElement("Config"); // 创建根节点
doc.AppendChild(root); // 加入到xml document
XmlElement FillItem; // 创建FillItem元素
FillItem = doc.CreateElement("ProgramInfo");
root.AppendChild(FillItem); // 添加到xml document
XmlElement Node = doc.CreateElement("CurrentPlayIndex");
Node.InnerText = app.currentIndex.ToString();
FillItem.AppendChild(Node);
Node = doc.CreateElement("PlayTime");
Node.InnerText = app.PlayTime.ToString();
FillItem.AppendChild(Node);
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, null);
writer.Formatting = System.Xml.Formatting.Indented;
doc.Save(writer);
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
stream.Position = 0;
string XMLString = sr.ReadToEnd();
doc.Save(@"config\\Program.xml"); // 保存文件
sr.Close();
stream.Close();
}
}
catch (Exception e)
{
log.WriteLogFile("WriteToProgramConfig:[" + e.ToString() + "]");
}
}
/// <summary>
/// /加载配置
/// </summary>
public void LoadProgramConfig()
{
try
{
string fpath = Directory.GetCurrentDirectory() + "\\config\\";
if (!Directory.Exists(fpath))
{
DirectoryInfo directoryInfo = new DirectoryInfo(fpath);
directoryInfo.Create();
}
string strConfigxml = AppDomain.CurrentDomain.BaseDirectory + "config//Program.xml";
if (File.Exists(strConfigxml) == true)
{
XmlDocument doc = new XmlDocument();
doc.Load(strConfigxml);
XmlNode xnFirst = doc.SelectSingleNode("Config");
XmlNode xnSecond = xnFirst.SelectSingleNode("ProgramInfo");
if (xnSecond == null)
{
log.WriteLogFile("配置文件错误-未发现ProgramInfo标签");
return;
}
else
{
app.currentIndex =Convert.ToInt32( xnSecond.SelectSingleNode("CurrentPlayIndex").InnerText);
app.currentIndex = app.currentIndex > 0 ? app.currentIndex - 1 : 0;
app.PlayTime =Convert.ToInt32( xnSecond.SelectSingleNode("PlayTime").InnerText);
}
}
else
{
XmlDocument doc = new XmlDocument(); // 创建dom对象
XmlDeclaration xmldecl;
xmldecl = doc.CreateXmlDeclaration("1.0", "gb2312", null);
doc.AppendChild(xmldecl);
XmlElement root = doc.CreateElement("Config"); // 创建根节点
doc.AppendChild(root); // 加入到xml document
XmlElement FillItem; // 创建FillItem元素
FillItem = doc.CreateElement("ProgramInfo");
root.AppendChild(FillItem); // 添加到xml document
XmlElement Node = doc.CreateElement("CurrentPlayIndex");
Node.InnerText = app.currentIndex.ToString();
FillItem.AppendChild(Node);
Node = doc.CreateElement("PlayTime");
Node.InnerText = app.PlayTime.ToString();
FillItem.AppendChild(Node);
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, null);
writer.Formatting = System.Xml.Formatting.Indented;
doc.Save(writer);
StreamReader sr = new StreamReader(stream, Encoding.UTF8);
stream.Position = 0;
string XMLString = sr.ReadToEnd();
doc.Save(@"config\\Program.xml"); // 保存文件
sr.Close();
stream.Close();
}
}
catch (Exception e)
{
log.WriteLogFile("LoadProgramConfig:[" + e.ToString() + "]");
}
}
}
}