C#实现动态桌面背景图片切换(续)

问题描述:昨天搞了一个自动切换桌面的小程序,用着不太方便。因为我每次添加新图片都是手动修改XML文件和重启服务,于是我搞了一个配置程序如下:

  上面展示的功能,基本上实现了程序所需要的功能。其中涉及的知识点有三个:

  (1):执行命令行语句,并且返回结果,这是我以前不知道的,以前我知道执行命令行命令,但是不知道如果返回结果,代码如下:

ExpandedBlockStart.gif 代码
         #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

   (2):XML写入和读取,以前搞过,好久不用,生疏了需要。现在代码记录如下,方便以后查找:

ExpandedBlockStart.gif 代码
         #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

 

  (3):安装服务,并且如何判断服务时候安装成功。如下:

 

ExpandedBlockStart.gif 代码
         #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

 

 

转载于:https://www.cnblogs.com/BlueWoods/archive/2010/03/11/1683584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值