利用ClickOnce手动更新和按需加载的一个DEMO

没时间整理:

namespace ClickOnce
{
    public partial class Form1 : Form
    {
        private string localVersion;
        private ApplicationDeployment deploy;
        private Dictionary<string, string> dllMapping = new Dictionary<string, string>();

        public Form1()
        {
            InitializeComponent();
            dllMapping.Add("OnDemandAssembly", "DemandAssembly01");
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

            deploy = ApplicationDeployment.CurrentDeployment;
            deploy.UpdateProgressChanged += new DeploymentProgressChangedEventHandler(deploy_UpdateProgressChanged);


        }

        Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            // 可以加上你的权限管理
            Assembly needAssembly = null;
            if (ApplicationDeployment.IsNetworkDeployed)
            {
              
                string[] nameParts = args.Name.Split(',');
                string dllName = nameParts[0];
                MessageBox.Show("AsseblyResolve Begin");
                MessageBox.Show(nameParts[0]);
                string dowloadGroupName = dllMapping[dllName];               
             
                try
                {
                    deploy.DownloadFileGroup(dowloadGroupName);
                }
                catch (DeploymentException de)
                {
                    MessageBox.Show("Error Download file {0}",dllName);
                }

                //下载后加重新LoadAssembly
                needAssembly = Assembly.LoadFile(Application.StartupPath + @"/" + dllName + ".dll");
            }
            return (needAssembly);
        }

        void deploy_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
        {
            //this.progressBar1.Value =
            this.Text = String.Format("Downloading file group {0}; {1:D}K of {2:D}K completed.", e.Group, e.BytesCompleted / 1024, e.BytesTotal / 1024);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            LocalAppInfo();
            this.textBox1.Text = Application.StartupPath;
            DeployServerInfo();
            if (deploy.IsFirstRun)
            {
                MessageBox.Show("Thanks for choose kw");
            }
           
        }
        private void LocalAppInfo()
        {           
            this.localVersion = Assembly.GetExecutingAssembly().GetName().Name;
            this.localVersion += Assembly.GetExecutingAssembly().GetName().Version.ToString();
        }

        private void DeployServerInfo()
        {
            this.label3.Text += deploy.DataDirectory;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("开始更新检测");
            this.lbServerVersion.Text = deploy.CurrentVersion.ToString();

            UpdateCheckInfo checkInfo = deploy.CheckForDetailedUpdate();
            if (checkInfo.UpdateAvailable)
            {
                deploy.Update();
                MessageBox.Show("Update Ok");
                Application.Restart();
            }

            //if (deploy.CheckForUpdate())
            //{
            //    deploy.UpdateAsync();
            //}
            //else
            //{
            //    this.Text = "the version that running is the newest verions";
            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DynamicClass dc = new DynamicClass();
            MessageBox.Show(dc.Message);
         
        }

     //注意:数据文件ClickOnce发布以后位置的变化

       private void button3_Click(object sender, EventArgs e)
        {   
           // 会报错
            if (File.Exists("UserInfo.xml"))
            {
                XmlDataDocument doc = new XmlDataDocument();
                StreamReader sr = new StreamReader("UserInfo.xml");
                doc.DataSet.ReadXml(sr);
                tb = doc.DataSet.Tables[0];
                this.dataGridView1.DataSource = tb;
                sr.Close();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            XmlDataDocument doc = new XmlDataDocument(tb);
            doc.Save("e://UserInfo.xml");
        }
    }

参考:http://blog.joycode.com/ghj/archive/2007/06/14/104220.aspx 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值