當前位置: 首頁>>代碼示例>>C#>>正文


C# ApplicationDeployment.DownloadFileGroup方法代碼示例

本文整理匯總了C#中System.Deployment.Application.ApplicationDeployment.DownloadFileGroup方法的典型用法代碼示例。如果您正苦於以下問題:C# ApplicationDeployment.DownloadFileGroup方法的具體用法?C# ApplicationDeployment.DownloadFileGroup怎麽用?C# ApplicationDeployment.DownloadFileGroup使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Deployment.Application.ApplicationDeployment的用法示例。


在下文中一共展示了ApplicationDeployment.DownloadFileGroup方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

namespace ClickOnceOnDemand
{
    [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Unrestricted=true)]
    public class Form1 : Form
    {
        // Maintain a dictionary mapping DLL names to download file groups. This is trivial for this sample,
        // but will be important in real-world applications where a feature is spread across multiple DLLs,
        // and you want to download all DLLs for that feature in one shot. 
        Dictionary<String, String> DllMapping = new Dictionary<String, String>();

        public static void Main()
        {
            Form1 NewForm = new Form1();
            Application.Run(NewForm);
        }

        public Form1()
        {
            // Configure form. 
            this.Size = new Size(500, 200);
            Button getAssemblyButton = new Button();
            getAssemblyButton.Size = new Size(130, getAssemblyButton.Size.Height);
            getAssemblyButton.Text = "Test Assembly";
            getAssemblyButton.Location = new Point(50, 50);
            this.Controls.Add(getAssemblyButton);
            getAssemblyButton.Click += new EventHandler(getAssemblyButton_Click);

            DllMapping["ClickOnceLibrary"] = "ClickOnceLibrary";
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        }

        /*
         * Use ClickOnce APIs to download the assembly on demand.
         */
        private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            Assembly newAssembly = null;

            if (ApplicationDeployment.IsNetworkDeployed)
            {
                ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;

                // Get the DLL name from the Name argument.
                string[] nameParts = args.Name.Split(',');
                string dllName = nameParts[0];
                string downloadGroupName = DllMapping[dllName];

                try
                {
                    deploy.DownloadFileGroup(downloadGroupName);
                }
                catch (DeploymentException de)
                {
                    MessageBox.Show("Downloading file group failed. Group name: " + downloadGroupName + "; DLL name: " + args.Name);
                    throw (de);
                }

                // Load the assembly.
                // Assembly.Load() doesn't work here, as the previous failure to load the assembly
                // is cached by the CLR. LoadFrom() is not recommended. Use LoadFile() instead.
                try
                {
                    newAssembly = Assembly.LoadFile(Application.StartupPath + @"\" + dllName + ".dll," +  
            "Version=1.0.0.0, Culture=en, PublicKeyToken=03689116d3a4ae33");
                }
                catch (Exception e)
                {
                    throw (e);
                }
            }
            else
            {
                //Major error - not running under ClickOnce, but missing assembly. Don't know how to recover.
                throw (new Exception("Cannot load assemblies dynamically - application is not deployed using ClickOnce."));
            }

            return (newAssembly);
        }

        private void getAssemblyButton_Click(object sender, EventArgs e)
        {
            DynamicClass dc = new DynamicClass();
            MessageBox.Show("Message: " + dc.Message);
        }
    }
}
開發者ID:.NET開發者,項目名稱:System.Deployment.Application,代碼行數:86,代碼來源:ApplicationDeployment.DownloadFileGroup


注:本文中的System.Deployment.Application.ApplicationDeployment.DownloadFileGroup方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。