Provides instance methods for the creation, modification, and deletion of file extension associations in the Windows registry.
Esempio n. 1
0
        /// <summary>
        /// Gets or value that determines the <see cref="PerceivedType"/>PerceivedType of the file.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns><see cref="PerceivedTypes"/> that specifies Perceived Type of extension.</returns>
        protected PerceivedTypes GetPerceivedType(FileAssociationInfo file)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            object         val        = registryWrapper.Read(file.extension, "PerceivedType");
            PerceivedTypes actualType = PerceivedTypes.None;

            if (val == null)
            {
                return(actualType);
            }

            try
            {
                actualType = (PerceivedTypes)Enum.Parse(typeof(PerceivedTypes), val.ToString(), true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }


            return(actualType);
        }
Esempio n. 2
0
        /// <summary>
        /// Sets array of containing program file names which should be displayed in the Open With List.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="programList">Program file names</param>
        protected void SetOpenWithList(FileAssociationInfo file, string[] programList)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            RegistryKey root = Registry.ClassesRoot;

            RegistryKey key = root.OpenSubKey(file.extension, true);

            RegistryKey tmpkey = key.OpenSubKey("OpenWithList", true);

            if (tmpkey != null)
            {
                key.DeleteSubKeyTree("OpenWithList");
            }

            key = key.CreateSubKey("OpenWithList");

            foreach (string s in programList)
            {
                key.CreateSubKey(s);
            }

            ShellNotification.NotifyOfChange();
        }
Esempio n. 3
0
        /// <summary>
        /// Creates actual extension association key in registry for the specified extension and supplied attributes.
        /// </summary>
        /// <param name="progId">Name of expected handling program.</param>
        /// <param name="perceivedType"><see cref="PerceivedTypes"/>PerceivedType of file type.</param>
        /// <param name="contentType">MIME type of file type.</param>
        /// <param name="openwithList"></param>
        /// <returns>FileAssociationInfo instance referring to specified extension.</returns>
        public FileAssociationInfo Create(string progId, PerceivedTypes perceivedType, string contentType, string[] openwithList)
        {
            FileAssociationInfo fai = new FileAssociationInfo(extension);

            if (fai.Exists)
            {
                fai.Delete();
            }

            fai.Create();
            fai.ProgID = progId;

            if (perceivedType != PerceivedTypes.None)
            {
                fai.PerceivedType = perceivedType;
            }

            if (contentType != string.Empty)
            {
                fai.ContentType = contentType;
            }

            if (openwithList != null)
            {
                fai.OpenWithList = openwithList;
            }

            return(fai);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates actual file extension entry in registry.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> instance that contains specifics on extension to be created.</param>
        protected void Create(FileAssociationInfo file)
        {
            if (file.Exists)
            {
                file.Delete();
            }

            RegistryKey root = Registry.ClassesRoot;

            root.CreateSubKey(file.extension);
        }
Esempio n. 5
0
        /// <summary>
        /// Set a value that indicates the name of the associated application with the behavior to handle this extension.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="progId">Associated Program ID of handling program.</param>
        protected void SetProgID(FileAssociationInfo file, string progId)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            registryWrapper.Write(file.extension, string.Empty, progId);

            ShellNotification.NotifyOfChange();
        }
Esempio n. 6
0
        /// <summary>
        /// Sets a value that determines the MIME type of the file.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="type">MIME content type of extension.</param>
        protected void SetContentType(FileAssociationInfo file, string type)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            registryWrapper.Write(file.extension, "Content Type", type);

            ShellNotification.NotifyOfChange();
        }
Esempio n. 7
0
        /// <summary>
        /// Sets a value that determines the <see cref="PerceivedType"/>PerceivedType of the file.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="type"><see cref="PerceivedTypes"/> to be set that specifies Perceived Type of extension.</param>
        protected void SetPerceivedType(FileAssociationInfo file, PerceivedTypes type)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            registryWrapper.Write(file.extension, "PerceivedType", type.ToString());

            ShellNotification.NotifyOfChange();
        }
Esempio n. 8
0
        /// <summary>
        /// Deletes actual file extension entry in registry.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> instance that contains specifics on extension to be deleted.</param>
        protected void Delete(FileAssociationInfo file)
        {
            if (!file.Exists)
            {
                throw new Exception("Key not found.");
            }

            RegistryKey root = Registry.ClassesRoot;

            root.DeleteSubKeyTree(file.extension);
        }
Esempio n. 9
0
        /// <summary>
        /// Associates an already existing program id with a list of extensions.
        /// </summary>
        /// <param name="progId">The program id to associate extensions with.</param>
        /// <param name="extensions">String array of extensions to associate with program id.</param>
        public void Associate(string progId, params string[] extensions)
        {
            foreach (string s in extensions)
            {
                FileAssociationInfo fai = new FileAssociationInfo(s);

                if (!fai.Exists)
                    fai.Create(progId);

                fai.ProgID = progId;
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Associates an already existing program id with a list of extensions.
        /// </summary>
        /// <param name="progId">The program id to associate extensions with.</param>
        /// <param name="extensions">String array of extensions to associate with program id.</param>
        public void Associate(string progId, params string[] extensions)
        {
            foreach (string s in extensions)
            {
                FileAssociationInfo fai = new FileAssociationInfo(s);

                if (!fai.Exists)
                {
                    fai.Create(progId);
                }

                fai.ProgID = progId;
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Gets a value that indicates the name of the associated application with the behavior to handle this extension.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns>Associated Program ID of handling program.</returns>
        protected string GetProgID(FileAssociationInfo file)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            object val = registryWrapper.Read(file.extension, string.Empty);

            if (val == null)
            {
                return(string.Empty);
            }

            return(val.ToString());
        }
Esempio n. 12
0
        /// <summary>
        /// Sets a value that indicates the filter component that is used to search for text within documents of this type.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="persistentHandler">Guid of filter component.</param>
        protected void SetPersistentHandler(FileAssociationInfo file, Guid persistentHandler)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            if (persistentHandler == Guid.Empty)
            {
                return;
            }

            this.registryWrapper.Write(file.extension + "\\" + PersistentHandler, string.Empty, persistentHandler);

            ShellNotification.NotifyOfChange();
        }
Esempio n. 13
0
        /// <summary>
        /// Verifies that given extension exists and is associated with given program id
        /// </summary>
        /// <param name="extension">Extension to be checked for.</param>
        /// <param name="progId">progId to be checked for.</param>
        /// <returns>True if association exists, false if it does not.</returns>
        public bool IsValid(string extension, string progId)
        {
            FileAssociationInfo fai = new FileAssociationInfo(extension);

            if (!fai.Exists)
            {
                return(false);
            }

            if (progId != fai.ProgID)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 14
0
        /// <summary>
        /// Determines of the list of extensions are associated with the specified program id.
        /// </summary>
        /// <param name="progId">Program id to check against.</param>
        /// <param name="extensions">String array of extensions to check against the program id.</param>
        /// <returns>String array of extensions that were not associated with the program id.</returns>
        public string[] CheckAssociation(string progId, params string[] extensions)
        {
            List <string> notAssociated = new List <string>();

            foreach (string s in extensions)
            {
                FileAssociationInfo fai = new FileAssociationInfo(s);

                if (!fai.Exists || fai.ProgID != progId)
                {
                    notAssociated.Add(s);
                }
            }

            return(notAssociated.ToArray());
        }
Esempio n. 15
0
        /// <summary>
        /// Gets a value that indicates the filter component that is used to search for text within documents of this type.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns>Guid of filter component.</returns>
        protected Guid GetPersistentHandler(FileAssociationInfo file)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            object val = registryWrapper.Read(file.extension + "\\PersistentHandler", string.Empty);

            if (val == null)
            {
                return(new Guid());
            }
            else
            {
                return(new Guid(val.ToString()));
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Associates a single executable with a list of extensions.
        /// </summary>
        /// <param name="progId">Name of program id</param>
        /// <param name="executablePath">Path to executable to start including arguments.</param>
        /// <param name="extensions">String array of extensions to associate with program id.</param>
        /// <example>progId = "MyTextFile"
        /// executablePath = "notepad.exe %1"
        /// extensions = ".txt", ".text"</example>
        public void Associate(string progId, string executablePath, params string[] extensions)
        {
            foreach (string s in extensions)
            {
                FileAssociationInfo fai = new FileAssociationInfo(s);

                if (!fai.Exists)
                    fai.Create(progId);

                fai.ProgID = progId;
            }

            ProgramAssociationInfo pai = new ProgramAssociationInfo(progId);

            if (!pai.Exists)
                pai.Create();

            pai.AddVerb(new ProgramVerb("open", executablePath));
        }
Esempio n. 17
0
        /// <summary>
        /// Gets array of containing program file names which should be displayed in the Open With List.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns>Program file names</returns>
        protected string[] GetOpenWithList(FileAssociationInfo file)
        {
            if (!file.Exists)
            {
                throw new Exception("Extension does not exist");
            }

            RegistryKey root = Registry.ClassesRoot;

            RegistryKey key = root.OpenSubKey(file.extension);

            key = key.OpenSubKey("OpenWithList");

            if (key == null)
            {
                return(new string[0]);
            }

            return(key.GetSubKeyNames());
        }
Esempio n. 18
0
        /// <summary>
        /// Associates a single executable with a list of extensions.
        /// </summary>
        /// <param name="progId">Name of program id</param>
        /// <param name="executablePath">Path to executable to start including arguments.</param>
        /// <param name="extensions">String array of extensions to associate with program id.</param>
        /// <example>progId = "MyTextFile"
        /// executablePath = "notepad.exe %1"
        /// extensions = ".txt", ".text"</example>
        public void Associate(string progId, string executablePath, params string[] extensions)
        {
            foreach (string s in extensions)
            {
                FileAssociationInfo fai = new FileAssociationInfo(s);

                if (!fai.Exists)
                {
                    fai.Create(progId);
                }

                fai.ProgID = progId;
            }

            ProgramAssociationInfo pai = new ProgramAssociationInfo(progId);

            if (!pai.Exists)
            {
                pai.Create();
            }

            pai.AddVerb(new ProgramVerb("open", executablePath));
        }
Esempio n. 19
0
        /// <summary>
        /// Gets a value that indicates the name of the associated application with the behavior to handle this extension.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns>Associated Program ID of handling program.</returns>
        protected string GetProgID(FileAssociationInfo file)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            object val = registryWrapper.Read(file.extension, string.Empty);

            if (val == null)
                return string.Empty;

            return val.ToString();
        }
Esempio n. 20
0
        /// <summary>
        /// Gets a value that indicates the filter component that is used to search for text within documents of this type.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns>Guid of filter component.</returns>
        protected Guid GetPersistentHandler(FileAssociationInfo file)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            object val = registryWrapper.Read(file.extension + "\\PersistentHandler", string.Empty);

            if (val == null)
                return new Guid();
            else
                return new Guid(val.ToString());
        }
Esempio n. 21
0
        /// <summary>
        /// Gets or value that determines the <see cref="PerceivedType"/>PerceivedType of the file.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns><see cref="PerceivedTypes"/> that specifies Perceived Type of extension.</returns>
        protected PerceivedTypes GetPerceivedType(FileAssociationInfo file)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            object val = registryWrapper.Read(file.extension, "PerceivedType");
            PerceivedTypes actualType = PerceivedTypes.None;

            if (val == null)
                return actualType;

            try
            {
                actualType = (PerceivedTypes)Enum.Parse(typeof(PerceivedTypes), val.ToString(), true);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return actualType;
        }
Esempio n. 22
0
        /// <summary>
        /// Gets array of containing program file names which should be displayed in the Open With List.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns>Program file names</returns>
        protected string[] GetOpenWithList(FileAssociationInfo file)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            RegistryKey root = Registry.ClassesRoot;

            RegistryKey key = root.OpenSubKey(file.extension);

            key = key.OpenSubKey("OpenWithList");

            if (key == null)
            {
                return new string[0];
            }

            return key.GetSubKeyNames();
        }
Esempio n. 23
0
        /// <summary>
        /// Set a value that indicates the name of the associated application with the behavior to handle this extension.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="progId">Associated Program ID of handling program.</param>
        protected void SetProgID(FileAssociationInfo file, string progId)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            registryWrapper.Write(file.extension, string.Empty, progId);

            ShellNotification.NotifyOfChange();
        }
Esempio n. 24
0
        /// <summary>
        /// Sets a value that determines the <see cref="PerceivedType"/>PerceivedType of the file.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="type"><see cref="PerceivedTypes"/> to be set that specifies Perceived Type of extension.</param>
        protected void SetPerceivedType(FileAssociationInfo file, PerceivedTypes type)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            registryWrapper.Write(file.extension, "PerceivedType", type.ToString());

            ShellNotification.NotifyOfChange();
        }
Esempio n. 25
0
        /// <summary>
        /// Verifies that given extension exists and is associated with given program id
        /// </summary>
        /// <param name="extension">Extension to be checked for.</param>
        /// <param name="progId">progId to be checked for.</param>
        /// <returns>True if association exists, false if it does not.</returns>
        public bool IsValid(string extension, string progId)
        {
            FileAssociationInfo fai = new FileAssociationInfo(extension);

            if (!fai.Exists)
                return false;

            if (progId != fai.ProgID)
                return false;

            return true;
        }
Esempio n. 26
0
        /// <summary>
        /// Creates actual extension association key in registry for the specified extension and supplied attributes.
        /// </summary>
        /// <param name="progId">Name of expected handling program.</param>
        /// <param name="perceivedType"><see cref="PerceivedTypes"/>PerceivedType of file type.</param>
        /// <param name="contentType">MIME type of file type.</param>
        /// <param name="openwithList"></param>
        /// <returns>FileAssociationInfo instance referring to specified extension.</returns>
        public FileAssociationInfo Create(string progId, PerceivedTypes perceivedType, string contentType, string[] openwithList)
        {
            FileAssociationInfo fai = new FileAssociationInfo(extension);

            if (fai.Exists)
            {
                fai.Delete();
            }

            fai.Create();
            fai.ProgID = progId;

            if (perceivedType != PerceivedTypes.None)
                fai.PerceivedType = perceivedType;

            if (contentType != string.Empty)
                fai.ContentType = contentType;

            if (openwithList != null)
                fai.OpenWithList = openwithList;

            return fai;
        }
Esempio n. 27
0
        /// <summary>
        /// Determines of the list of extensions are associated with the specified program id.
        /// </summary>
        /// <param name="progId">Program id to check against.</param>
        /// <param name="extensions">String array of extensions to check against the program id.</param>
        /// <returns>String array of extensions that were not associated with the program id.</returns>
        public string[] CheckAssociation(string progId, params string[] extensions)
        {
            List<string> notAssociated = new List<string>();

            foreach (string s in extensions)
            {
                FileAssociationInfo fai = new FileAssociationInfo(s);

                if (!fai.Exists || fai.ProgID != progId)
                    notAssociated.Add(s);
            }

            return notAssociated.ToArray();
        }
Esempio n. 28
0
        /// <summary>
        /// Sets a value that determines the MIME type of the file.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="type">MIME content type of extension.</param>
        protected void SetContentType(FileAssociationInfo file, string type)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            registryWrapper.Write(file.extension, "Content Type", type);

            ShellNotification.NotifyOfChange();
        }
Esempio n. 29
0
        /// <summary>
        /// Sets array of containing program file names which should be displayed in the Open With List.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="programList">Program file names</param>
        protected void SetOpenWithList(FileAssociationInfo file, string[] programList)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            RegistryKey root = Registry.ClassesRoot;

            RegistryKey key = root.OpenSubKey(file.extension, true);

            RegistryKey tmpkey = key.OpenSubKey("OpenWithList", true);

            if (tmpkey != null)
            {
                key.DeleteSubKeyTree("OpenWithList");
            }

            key = key.CreateSubKey("OpenWithList");

            foreach (string s in programList)
            {
                key.CreateSubKey(s);
            }

            ShellNotification.NotifyOfChange();
        }
Esempio n. 30
0
        /// <summary>
        /// Creates actual file extension entry in registry.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> instance that contains specifics on extension to be created.</param>
        protected void Create(FileAssociationInfo file)
        {
            if (file.Exists)
            {
                file.Delete();
            }

            RegistryKey root = Registry.ClassesRoot;

            root.CreateSubKey(file.extension);
        }
Esempio n. 31
0
        /// <summary>
        /// Sets a value that indicates the filter component that is used to search for text within documents of this type.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <param name="persistentHandler">Guid of filter component.</param>
        protected void SetPersistentHandler(FileAssociationInfo file, Guid persistentHandler)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            if (persistentHandler == Guid.Empty)
                return;

            this.registryWrapper.Write(file.extension + "\\" + PersistentHandler, string.Empty, persistentHandler);

            ShellNotification.NotifyOfChange();
        }
Esempio n. 32
0
        /// <summary>
        /// Deletes actual file extension entry in registry.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> instance that contains specifics on extension to be deleted.</param>
        protected void Delete(FileAssociationInfo file)
        {
            if (!file.Exists)
            {
                throw new Exception("Key not found.");
            }

            RegistryKey root = Registry.ClassesRoot;

            root.DeleteSubKeyTree(file.extension);
        }
Esempio n. 33
0
        /// <summary>
        /// Register file association
        /// </summary>
        /// <param name="exts">Extensions, for ex: *.png;*.jpg</param>
        /// <param name="appPath">Executable file</param>
        public static void RegisterAssociation(string appPath, string exts)
        {
            string[] ext_list = exts.Replace("*", "").Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            foreach (string ext in ext_list)
            {
                FileAssociationInfo fa = new FileAssociationInfo(ext);
                if (!fa.Exists)
                {
                    return;
                }

                ProgramAssociationInfo pa = new ProgramAssociationInfo(fa.ProgID);

                if (!pa.Exists)
                {
                    return;
                }

                ProgramVerb[] verbs = pa.Verbs;
                List<ProgramVerb> l = new List<ProgramVerb>();
                l.AddRange(verbs);

                //remove existed verb
                ProgramVerb openVerb = l.SingleOrDefault(v => v.Name == "open");
                if (openVerb != null)
                {
                    l.Remove(openVerb);
                }

                //add new value
                openVerb = new ProgramVerb("open", "\"" + appPath + "\" \"%1\"");
                l.Add(openVerb);

                //save & apply changes
                pa.Verbs = l.ToArray();

                GlobalSetting.SetConfig("ContextMenuExtensions", exts);
            }
        }
Esempio n. 34
0
        /// <summary>
        /// Gets a value that determines the MIME type of the file.
        /// </summary>
        /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param>
        /// <returns>MIME content type of extension.</returns>
        protected string GetContentType(FileAssociationInfo file)
        {
            if (!file.Exists)
                throw new Exception("Extension does not exist");

            object val = registryWrapper.Read(file.extension, "Content Type");

            if (val == null)
            {
                return string.Empty;
            }
            else
            {
                return val.ToString();
            }
        }