Click here to Skip to main content
15,897,371 members
Articles / Multimedia / GDI+

MyDownloader: A Multi-thread C# Segmented Download Manager

Rate me:
Please Sign up or sign in to vote.
4.95/5 (418 votes)
12 Feb 2008CPOL10 min read 2.2M   124.8K   1.3K  
Sample application that manages multiple segmented downloads and supports HTTP, FTP and YouTube video downloads
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Windows.Forms;
using System.IO;
using System.Security.Permissions;
using System.Security.AccessControl;

namespace MyDownloader.Extension.WindowsIntegration
{
    [RegistryPermission(SecurityAction.LinkDemand, 
       Read = "Software\\Microsoft\\Windows\\CurrentVersion\\Run",
       Write = "Software\\Microsoft\\Windows\\CurrentVersion\\Run")]
    internal class WindowsStartupUtility
    {
        private static string GetKeyName()
        {
            return Path.GetFileNameWithoutExtension(Application.ExecutablePath);
        }

        private static string GetKeyValue()
        {
            return String.Format("\"{0}\" /as", Application.ExecutablePath);
        }

        private static RegistryKey GetRegistryKey()
        {
            // Create a security context for a new key that we will use to store our data.
            // The security context will restrict access to only our user.
            string user = Environment.UserDomainName + "\\" + Environment.UserName;
            RegistrySecurity security = new RegistrySecurity();
            RegistryAccessRule rule = new RegistryAccessRule(user,
                    RegistryRights.FullControl,
                    InheritanceFlags.ContainerInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow);
            security.AddAccessRule(rule);

            RegistryKey key = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",
                RegistryKeyPermissionCheck.ReadWriteSubTree, security);

            return key;
        }

        public static bool IsRegistered()
        {
            using (RegistryKey key = GetRegistryKey())
            {
                string value = key.GetValue(GetKeyName(), null) as string;

                return value != null && value.Equals(GetKeyValue(), StringComparison.OrdinalIgnoreCase);
            }
        }

        public static void Register(bool register)
        {
            using (RegistryKey key = GetRegistryKey())
            {
                if (register)
                {
                    key.SetValue(GetKeyName(), GetKeyValue(), RegistryValueKind.String);
                }
                else
                {
                    key.DeleteValue(GetKeyName(), false);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Brazil Brazil
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions