Пример #1
0
    public static void Main()
    {
        // Initializes a new AF_FileAssociator to associate the .ABC file extension.
        AF_FileAssociator assoc = new AF_FileAssociator(".abc");

        // Creates a new file association for the .ABC file extension. Data is overwritten if it already exists.
        assoc.Create("My_App",
                     "My application's file association",
                     new ProgramIcon(@"C:\Program Files\My_App\icon.ico"),
                     new ExecApplication(@"C:\Program Files\My_App\myapp.exe"),
                     new OpenWithList(new string[] { "My_App" }));

        // Gets each piece of association info individually, all as strings.
        string id          = assoc.ID;
        string description = assoc.Description;
        string icon        = assoc.DefaultIcon.IconPath;
        string execApp     = assoc.Executable.Path;

        string[] openWithList = assoc.OpenWith.List;

        // Sets each peice of association info individually.
        ProgramIcon     newDefIcon  = new ProgramIcon(@"C:\Program Files\My_App\icon2.ico");
        ExecApplication newExecApp  = new ExecApplication(@"C:\Program Files\My_App\myapp2.exe");
        OpenWithList    newOpenWith = new OpenWithList(new string[] { "myapp2.exe" });

        assoc.ID          = "My_App_2";
        assoc.Description = "My application's file association #2";
        assoc.DefaultIcon = newDefIcon;
        assoc.Executable  = newExecApp;
        assoc.OpenWith    = newOpenWith;

        // Gets the extension of the associator that was set when initializing it.
        string extension = assoc.Extension;

        // Deletes any keys that were associated with the .ABC file extension.
        assoc.Delete();
    }
Пример #2
0
            /// <summary>
            /// Create or overwrite a current file association for this FileAssociator's set extension.
            /// </summary>
            /// <param name="progID">The basic application name that uses this file extension.</param>
            /// <param name="description">The desription of this file extension and/or program that uses it.</param>
            /// <param name="defaultIcon">The icon to show on the program and it's files.</param>
            /// <param name="execApp">The application that will be run when the file extension is clicked.</param>
            /// <param name="openWith">The programs that appear in the OpenWith list.</param>
            /// <exception cref="Exception">Thrown when an error occurs that will prevent it from working correctly.</exception>
            public void Create(string progID, string description, ProgramIcon defaultIcon, ExecApplication execApp, OpenWithList openWith)
            {
                if (progID != null)
                {
                    if (defaultIcon.IsValid && execApp.IsValid)
                    {
                        Registry.ClassesRoot.CreateSubKey(Extension).SetValue("", progID);
                        RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID,
                                                                            RegistryKeyPermissionCheck.ReadWriteSubTree);

                        if (description != null)
                        {
                            key.SetValue("", description, RegistryValueKind.String);
                        }

                        if (defaultIcon != null && defaultIcon.IsValid)
                        {
                            key.CreateSubKey("DefaultIcon").SetValue("", defaultIcon.IconPath, RegistryValueKind.String);
                        }
                        else
                        {
                            throw new Exception("The default icon you entered is either null or doesn't exist...");
                        }

                        if (execApp != null && execApp.IsValid)
                        {
                            key.CreateSubKey(@"Shell\Open\Command").SetValue("", execApp.Path + " %1", RegistryValueKind.String);
                        }
                        else
                        {
                            throw new Exception("The executable application you entered is either null or not an .exe format...");
                        }

                        if (openWith != null)
                        {
                            key = key.CreateSubKey("OpenWithList", RegistryKeyPermissionCheck.ReadWriteSubTree);
                            foreach (string file in openWith.List)
                            {
                                key.CreateSubKey(file);
                            }
                        }

                        key.Flush();
                        key.Close();
                    }
                    else
                    {
                        throw new Exception("Either the icon or executable application object is invalid...");
                    }
                }
                else
                {
                    throw new Exception("The program ID you entered is null...");
                }
            }