Export (0) Print
Expand All

ApplicationDeployment.DownloadFileGroup Method

Downloads a set of optional files on demand.

Namespace:  System.Deployment.Application
Assembly:  System.Deployment (in System.Deployment.dll)

[<PermissionSetAttribute(SecurityAction.Assert, Name = "FullTrust")>]
member DownloadFileGroup : 
        groupName:string -> unit

Parameters

groupName
Type: System.String

The named group of files to download. All files marked "optional" in a ClickOnce application require a group name.

ExceptionCondition
ArgumentNullException

The groupName parameter is null or zero-length.

In a ClickOnce application, the files marked "optional" in the application manifest are not downloaded during initialization or update. You can use the DownloadFileGroup method to download all of the files belonging to a named group on demand, so that they do not consume network resources and disk space until you are sure the user requires them. For example, certain users may need to use a data analysis package included in your application on a daily basis, while other users may never invoke it.

To use an assembly you have downloaded with DownloadFileGroup, you attach an event listener to the AssemblyResolve event on the CurrentDomain. For an example, see =Walkthrough: Downloading Assemblies on Demand with the ClickOnce Deployment API Using the Designer.

All files are downloaded to the ClickOnce application cache and are therefore isolated to the current version of the application. Let's say that after an application downloads a file group, the user installs a new version of the application, but later reverts to the previous version of the application. In this case, the previous version will still have the copies of the files it originally downloaded. For more information about accessing data files, see Accessing Local and Remote Data in ClickOnce Applications.

DownloadFileGroup works in partially trusted applications, that is, in any ClickOnce application running with restricted permission. However, if you attempt to load assemblies dynamically, your application will require full trust.

On-demand downloading of data files is currently not supported.

Your application will not be responsive to user input until the DownloadFileGroup method returns. If you need to download files without interrupting the user's workflow, use the DownloadFileGroupAsync method instead.

You cannot download a single file by its file name alone. To download a single file, assign it a group name in your ClickOnce deployment and download the group using this method.

The following code example demonstrates how to load an assembly on demand by listening for the AssemblyResolve event.

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);
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft