C#实现动态桌面背景图片切换(续)
问题描述:昨天搞了一个自动切换桌面的小程序,用着不太方便。因为我每次添加新图片都是手动修改XML文件和重启服务,于是我搞了一个配置程序如下:
上面展示的功能,基本上实现了程序所需要的功能。其中涉及的知识点有三个:
(1):执行命令行语句,并且返回结果,这是我以前不知道的,以前我知道执行命令行命令,但是不知道如果返回结果,代码如下:

#region 执行DOS命令,返回DOS命令的输出
/// <summary>
/// 执行DOS命令,返回DOS命令的输出
/// </summary>
/// <param name="dosCommand">dos命令</param>
/// <param name="milliseconds">等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待</param>
/// <returns>返回输出,如果发生异常,返回空字符串</returns>
public static string Execute(string dosCommand, int milliseconds)
{
string output = "";
if (dosCommand != null && dosCommand != "")
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + dosCommand;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
try
{
if (process.Start())
{
if (milliseconds == 0)
process.WaitForExit();
else
process.WaitForExit(milliseconds);
output = process.StandardOutput.ReadToEnd();
}
}
catch
{
}
finally
{
if (process != null)
process.Close();
}
}
return output;
}
#endregion
/// <summary>
/// 执行DOS命令,返回DOS命令的输出
/// </summary>
/// <param name="dosCommand">dos命令</param>
/// <param name="milliseconds">等待命令执行的时间(单位:毫秒),如果设定为0,则无限等待</param>
/// <returns>返回输出,如果发生异常,返回空字符串</returns>
public static string Execute(string dosCommand, int milliseconds)
{
string output = "";
if (dosCommand != null && dosCommand != "")
{
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + dosCommand;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
try
{
if (process.Start())
{
if (milliseconds == 0)
process.WaitForExit();
else
process.WaitForExit(milliseconds);
output = process.StandardOutput.ReadToEnd();
}
}
catch
{
}
finally
{
if (process != null)
process.Close();
}
}
return output;
}
#endregion
(2):XML写入和读取,以前搞过,好久不用,生疏了需要。现在代码记录如下,方便以后查找:

#region btnSave_Click
private void btnSave_Click(object sender, EventArgs e)
{
if (this.lstFilePath.Items.Count > 0)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_PATH);
xmlDoc.SelectSingleNode("paths").RemoveAll();
XmlNode root = xmlDoc.SelectSingleNode("paths");
XmlElement xmlEle = (XmlElement)xmlDoc.SelectSingleNode("paths");
xmlEle.SetAttribute("time", this.txtTime.Text);
for (int i = 0; i < this.lstFilePath.Items.Count; i++)
{
XmlElement xmle = xmlDoc.CreateElement("path");
xmle.InnerText = this.lstFilePath.Items[i].ToString();
root.AppendChild(xmle);
}
xmlDoc.Save(XML_PATH);
MessageBox.Show("保存成功!请重新启动服务(先关闭,在启动)才能生效");
}
}
#endregion
private void btnSave_Click(object sender, EventArgs e)
{
if (this.lstFilePath.Items.Count > 0)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_PATH);
xmlDoc.SelectSingleNode("paths").RemoveAll();
XmlNode root = xmlDoc.SelectSingleNode("paths");
XmlElement xmlEle = (XmlElement)xmlDoc.SelectSingleNode("paths");
xmlEle.SetAttribute("time", this.txtTime.Text);
for (int i = 0; i < this.lstFilePath.Items.Count; i++)
{
XmlElement xmle = xmlDoc.CreateElement("path");
xmle.InnerText = this.lstFilePath.Items[i].ToString();
root.AppendChild(xmle);
}
xmlDoc.Save(XML_PATH);
MessageBox.Show("保存成功!请重新启动服务(先关闭,在启动)才能生效");
}
}
#endregion
(3):安装服务,并且如何判断服务时候安装成功。如下:

#region btnInstall_Click
private void btnInstall_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(App_Path + "DynamicDesktop");
File.Copy(Environment.CurrentDirectory.ToString() + "/DynamicDesktop/BackGround.exe", App_Path + "DynamicDesktop\\BackGround.exe", true);
File.Copy(Environment.CurrentDirectory.ToString() + "/DynamicDesktop/BackGround.exe.manifest", App_Path + "DynamicDesktop\\BackGround.exe.manifest", true);
File.Copy(Environment.CurrentDirectory.ToString() + "/DynamicDesktop/蝴蝶.bmp", App_Path + "DynamicDesktop\\蝴蝶.bmp", true);
File.Copy(Environment.CurrentDirectory.ToString() + "/DynamicDesktop/desktop.xml", Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\desktop.xml", true);
if (!File.Exists(txtSDK.Text.Trim() + "\\installutil.exe"))
{
MessageBox.Show("请选择正确 v2.0 SDK 路径!");
return;
}
string result = string.Empty;
result = Execute(txtSDK.Text + "\\installutil.exe " + App_Path + "DynamicDesktop\\BackGround.exe",0);
if (result.Contains("已完成事务处理安装"))
{
MessageBox.Show("安装成功!");
InitShowDesktop();
}
else
{
MessageBox.Show(result);
}
}
#endregion
private void btnInstall_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(App_Path + "DynamicDesktop");
File.Copy(Environment.CurrentDirectory.ToString() + "/DynamicDesktop/BackGround.exe", App_Path + "DynamicDesktop\\BackGround.exe", true);
File.Copy(Environment.CurrentDirectory.ToString() + "/DynamicDesktop/BackGround.exe.manifest", App_Path + "DynamicDesktop\\BackGround.exe.manifest", true);
File.Copy(Environment.CurrentDirectory.ToString() + "/DynamicDesktop/蝴蝶.bmp", App_Path + "DynamicDesktop\\蝴蝶.bmp", true);
File.Copy(Environment.CurrentDirectory.ToString() + "/DynamicDesktop/desktop.xml", Environment.GetFolderPath(Environment.SpecialFolder.System) + "\\desktop.xml", true);
if (!File.Exists(txtSDK.Text.Trim() + "\\installutil.exe"))
{
MessageBox.Show("请选择正确 v2.0 SDK 路径!");
return;
}
string result = string.Empty;
result = Execute(txtSDK.Text + "\\installutil.exe " + App_Path + "DynamicDesktop\\BackGround.exe",0);
if (result.Contains("已完成事务处理安装"))
{
MessageBox.Show("安装成功!");
InitShowDesktop();
}
else
{
MessageBox.Show(result);
}
}
#endregion
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步