C# 远程更新 分类: .NET 20121228 11:05 1156人阅读 评论(0) 收藏

一段服务器端配置文件,一段客户端配置文件,一段下载代码。

RemoteXmlFile.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <Item key="client" value="20121228" content="tlnews001,tlnews002,tlnews003" size="3" />
  <Item key="renbao" value="20121228" content="a2.pdf,a1.pdf,a3.pdf,a4.pdf,a5.pdf,a6.pdf,a7.pdf,a8.pdf" size="4777360" />
  <Item key="cenbao" value="20121228" content="a1.pdf,a2.pdf,a4.pdf,a6.pdf,a3.pdf,a5.pdf,a8.pdf,a7.pdf,a10.pdf,a9.pdf,a11.pdf" size="5542552" />
  <Item key="button" value="20121124 10:59:52" content="" size="" />
  <Item key="banner" value="20121124 10:55:40" content="" size="" />
  <Item key="web" value="20121218 10:16:40" content="index.html" size="2116" />
  <Item key="pb" value="20121124 10:56:21" content="" size="" />
</configuration>


AnalysisXml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace comback
{
    class AnalysisXml
    {
        //更新本地配置文件节点值
        public void UpdateLocalXml(string xmlPath, string updateKey, string updateValue, string updateContent, string updateSize)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(xmlPath);

                foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                {
                    string key = node.Attributes["key"].Value;
                    if (key == updateKey)
                    {
                        node.Attributes["value"].Value = updateValue;
                        node.Attributes["content"].Value = updateContent;
                        node.Attributes["size"].Value = updateSize;
                        xDoc.Save(xmlPath);
                        break;
                    }
                }
            }
            catch (Exception ee)
            {
                System.Windows.Forms.MessageBox.Show("在AnalysisXml类中操作UpdateLocalXml时异常:" + ee.Message);
            }
        }
    }
}


ClientXmlFile.xml

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <Item key="renbao" value="20121219" />
  <Item key="cenbao" value="20121219" />
  <Item key="button" value="20121124 10:59:52" />
  <Item key="banner" value="20121124 10:55:40" />
  <Item key="web" value="20121218 10:16:40" />
  <Item key="pb" value="20121124 10:56:21" />
</configuration>


AnalysisXml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace ClientIIS
{
    //解析xml配置文件
    class AnalysisXml
    {
        //获取app.config值
        public string GetValue(string appKey)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(Application.StartupPath + "\\App.config");
                XmlNode xNode;
                XmlElement xElem;
                xNode = xDoc.SelectSingleNode("//appSettings");
                xElem = (XmlElement)xNode.SelectSingleNode("//add[@key='" + appKey + "']");
                if (xElem != null)
                    return xElem.GetAttribute("value");
                else
                    return "";
            }
            catch (Exception ee)
            {
                LogManager.WriteLog(LogFile.XmlError, "", "在AnalysisXml类中操作GetValue时异常:" + ee.Message);
            }
            return "";
        }

        //修改远程文件(.ini)
        public void UpdateRemoteFile(string xmlPath, string updateKey, string updateValue)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(xmlPath);

                foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                {
                    string key = node.Attributes["key"].Value;
                    if (key == updateKey)
                    {
                        node.Attributes["value"].Value = updateValue;
                        xDoc.Save(xmlPath);
                        MessageBox.Show("修改成功!");
                        break;
                    }
                }
            }
            catch (Exception ee)
            {
                LogManager.WriteLog(LogFile.XmlError, "", "在AnalysisXml类中操作UpdateLocalXml时异常:" + ee.Message);
            }
        }

        List<ClientFileInfo> remoteList = new List<ClientFileInfo>();
        //获取远程配置文件信息
        public List<ClientFileInfo> GetRemoteXmlValue(string xmlPath)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(xmlPath);
               
                foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                {
                    string key = node.Attributes["key"].Value;
                    string value = node.Attributes["value"].Value;
                    string content = node.Attributes["content"].Value;
                    string size = node.Attributes["size"].Value;

                    //我想到了一个温柔的你,可以用你来抚平创伤
                    if (string.IsNullOrEmpty(content) || string.IsNullOrEmpty(size))
                    {
                        continue;
                    }

                    ClientFileInfo file = new ClientFileInfo(key, value, content, size);
                    remoteList.Add(file);
                }
            }
            catch (Exception ee)
            {
                LogManager.WriteLog(LogFile.XmlError, "", "在AnalysisXml类中操作GetRemoteXmlValue时异常:" + ee.Message);  
            }
            return remoteList;
        }

        //获取本地配置文件信息
        List<ClientFileInfo> localList = new List<ClientFileInfo>();
        public List<ClientFileInfo> GetLocalXmlValue(string xmlPath)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(xmlPath);

                foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                {
                    string key = node.Attributes["key"].Value;
                    string value = node.Attributes["value"].Value;

                    ClientFileInfo file = new ClientFileInfo(key, value);
                    localList.Add(file);
                }
            }
            catch (Exception ee)
            {
                LogManager.WriteLog(LogFile.XmlError, "", "在AnalysisXml类中操作GetLocalXmlValue时异常:" + ee.Message);
            }
            return localList;
        }

        //更新本地配置文件节点值
        public void UpdateLocalXml(string xmlPath, string updateKey,string updateValue)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.Load(xmlPath);

                foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)
                {
                    string key = node.Attributes["key"].Value;
                    if (key == updateKey)
                    {
                        node.Attributes["value"].Value = updateValue;
                        xDoc.Save(xmlPath);
                        break;
                    }
                }
            }
            catch (Exception ee)
            {
                LogManager.WriteLog(LogFile.XmlError, "", "在AnalysisXml类中操作UpdateLocalXml时异常:" + ee.Message);
            }
        }
    }
}

 private void DownloadFile()
        {
            try
            {
                if (updateFileList != null && updateFileList.Count > 0)
                {
                    for (int i = 0; i < updateFileList.Count; i++)
                    {
                        string downFile = this.updateFileList[i];
                        string saveFile = this.saveFileList[i];

                        clientDownload = new WebClient();
                        if (clientDownload.IsBusy)
                        {
                            clientDownload.CancelAsync();
                        }
                        clientDownload.DownloadFileAsync(new Uri(downFile), saveFile, saveFile);
                        clientDownload.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                    }
                }
            }
            catch(Exception ee)
            {
                LogManager.WriteLog(LogFile.OtherError, "", "在程序ClientIIS的ClientIISForm类中操作DownloadFile时异常:" + ee.Message);
            }
        }



版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/configman/p/4657569.html