前言
上次技师分享了通过修改计算机注册表的方式,用json这种配置文件来实现设置开机是否自动启动,这次我们使用xml配置文件来设置是否开机自启动
提示:以下是本篇文章正文内容,下面案例可供参考
一、GUI 设置
这次我们不使用checkbox控件,我们使用radiobutton来试试,在一个groupbox中放入两个radiobutton,一个命名为rbAutoLaunchTrue,另一个命名为rbAutoLaunchFalse
二、编写xml文件
<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<settingNo>ping0401</settingNo>
<appSetting>
<!--<seting Name="IsAutoLaunch" Value="false" Des="是否开机自启动"/>-->
<seting IsAutoLaunch="False" Des="是否开机自启动" />
</appSetting>
</appSettings>
三.逻辑代码(和json版本类似)
代码如下(示例):
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
namespace AutoLaunch_XML
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
rbAutoLaunchTrue.Checked = ReadXML("AutoLaunch");
}
private bool ReadXML(string key)
{
XmlDocument doc = new XmlDocument();
string path = Application.StartupPath + @"\appSettings.xml";
//没有文件就先创建一个,默认不开机自启动
if(!File.Exists(path))
{
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
XmlElement appSettings = doc.CreateElement("appSettings");
doc.AppendChild(appSettings);
XmlElement settingNo = doc.CreateElement("settingNo");
settingNo.InnerText = "ping0401";
XmlElement appSetting = doc.CreateElement("appSetting");
appSettings.AppendChild(appSetting);
XmlElement seting1 = doc.CreateElement("setting");
// 给节点添加属性
//seting1.SetAttribute("Name", "IsAutoLaunch");
seting1.SetAttribute("IsAutoLaunch", "false");
seting1.SetAttribute("Des", "是否开机自启动");
appSetting.AppendChild(seting1);
doc.Save(path);
return true;
}
// 存在xml文件就读取
// 加载xml文档
doc.Load(path);
XmlNodeList xnl = doc.SelectNodes("/appSettings/appSetting/seting");
string str = xnl[0].Attributes["IsAutoLaunch"].Value;
return bool.Parse(str);
}
private void rbAutoLaunchTrue_CheckedChanged(object sender, EventArgs e)
{
string execPath = Application.ExecutablePath;
string execFileName = Path.GetFileName(execPath);
string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
RegistryKey rk = Registry.LocalMachine.OpenSubKey(regPath, true);
if (rk == null)
{
rk = Registry.LocalMachine.CreateSubKey(regPath);
}
if (rbAutoLaunchTrue.Checked)
{
rk.SetValue(execFileName, execPath);
}
else
{
rk.DeleteValue(execFileName, false);
}
rk.Close();
// 持久化到xml文件中
WriteXML("IsAutoLaunch", rbAutoLaunchTrue.Checked);
}
private void WriteXML(string key, bool @checked)
{
XmlDocument doc = new XmlDocument();
string path = Application.StartupPath + @"\appSettings.xml";
//没有文件就先创建一个,默认不开机自启动
if (!File.Exists(path))
{
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.AppendChild(dec);
XmlElement appSettings = doc.CreateElement("appSettings");
doc.AppendChild(appSettings);
XmlElement settingNo = doc.CreateElement("settingNo");
settingNo.InnerText = "ping0401";
XmlElement appSetting = doc.CreateElement("appSetting");
appSettings.AppendChild(appSetting);
XmlElement seting1 = doc.CreateElement("setting");
// 给节点添加属性
//seting1.SetAttribute("Name", "IsAutoLaunch");
seting1.SetAttribute("IsAutoLaunch", "false");
seting1.SetAttribute("Des", "是否开机自启动");
appSetting.AppendChild(seting1);
doc.Save(path);
}
// 存在就读取并修改
doc.Load(path);
XmlNodeList xnl = doc.SelectNodes("/appSettings/appSetting/seting");
xnl[0].Attributes["IsAutoLaunch"].Value = @checked.ToString();
doc.Save(path);
}
}
}
总结
开机自启动在前面一篇文章中说过了,这里主要涉及到对xml文件的读取,写入,修改等操作。这个例子中还有大量的冗余代码,大家用的时候可以提取方法,提取字段,优化一波哦o( ̄▽ ̄)ブ
另外,有个设置可以在编译时就把xml配置文件拷贝到debug文件夹中
复制到输出目录 -->如果教新则复制