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

原创 2007年10月11日 10:48:00

没时间整理:

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 

使用VS2005的 ClickOnce 技术实现按需下载组件

默认情况下,首次运行 ClickOnce 应用程序时,会下载该应用程序中包含的所有程序集。但是一些特殊的场景我们可能不希望它这么干,而是希望按照一定规测或者需要用到某个组件的时候再下载。比如我们可能会...
  • holyrong
  • holyrong
  • 2007-11-20 14:49:00
  • 553

dubbo + assembly 整理

提供dubbo服务启动、停止管理平台。 通过WAR项目管理dubbo服务
  • u013275741
  • u013275741
  • 2015-11-13 16:39:55
  • 2221

ClickOnce按需加载

  • 2009年08月06日 10:51
  • 57KB
  • 下载

如何让ClickOnce进行手动更新

默认为自动更新,不过我认为手动更新更加的合理化一些.想手动更新,只要在UPDATE中关闭"The application should check for updates"选项就可以了.所以专门贴出手...
  • ggw128
  • ggw128
  • 2008-03-05 14:55:00
  • 2569

maven打包样例

maven打包正常同时使用阿里的仓库加载jar包快速可以使用一下样例 实际中使用。&amp;lt;project xmlns=&quot;http://maven.apache.org/POM/4.0...
  • liuxiangke0210
  • liuxiangke0210
  • 2018-02-26 16:18:43
  • 35

Build and Deploy a .NET COM Assembly

Refernce Url:http://www.simple-talk.com/dotnet/visual-studio/build-and-deploy-a-.net-com-assembly/Bu...
  • figerdeng
  • figerdeng
  • 2009-07-22 15:10:00
  • 811

如何让ClickOnce进行手动更新(含代码)

方法一: 该方法出至microsoft官方,以下是C#代码:private void InstallUpdateSyncWithInfo() { UpdateCheckInfo info = ...
  • qq395537505
  • qq395537505
  • 2016-03-02 10:43:51
  • 789

C#Winform程序如何使用ClickOnce发布并自动升级(图解)

有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布 关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解) 其实打包是打包...
  • qq395537505
  • qq395537505
  • 2016-02-26 18:53:16
  • 5232

终于可以实现后台自动更新了

 高兴啊。。。。。。。。。。。。。下面是所有步骤。研究了一上午终于搞定!Step 1:  Build the application to updateIn this step we will bui...
  • dhz123
  • dhz123
  • 2004-11-02 12:29:00
  • 831

由ClickOnce部署的应用程序的两种模式和卸载问题

在项目的发布选项卡中可以为应用程序选择两种ClickOnce部署模式:   该应用程序只能联机使用   该应用程序也可以脱机使用(可以从“开始”菜单启动)   在这里,我们姑且分别称...
  • qq798833488
  • qq798833488
  • 2016-09-12 15:27:06
  • 1179
收藏助手
不良信息举报
您举报文章:利用ClickOnce手动更新和按需加载的一个DEMO
举报原因:
原因补充:

(最多只允许输入30个字)