C# 远程更新 分类: .NET 2012-12-2...

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

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/archive/2012/12/28/4657569.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值