Řešení věčného přeregistrovávání DDE

Ačkoli se snažím pořád dokola přesvědčovat Windows, aby mi zaregistrovaly příponu .DOC na aplikaci WINWORD.EXE bez DDE, které neúměrně zpomaluje start programů z Office, stále dokola se mi to předělává zpět a položka DDE se imrvére aktivuje. Nevím jak, nevím proč, ale pohár přetekl. Následující prográmek napsaný v C# provede přeregistrování potřebných přípon a odstraní inkriminovanou volbu DDE.

Prográmek musíte překompilovat a v metodě Main na konci souboru ještě předtím zkontrolujte, zda cesta k programům WINWORD.EXE, EXCEL.EXE a POWERPNT.EXE je správná. Kompilaci provedete příkazem:

csc Soubor.cs

Poté program stačí spustit. Není třeba se bát – program nejdříve zkontroluje, zda asociace již existuje, poté si uloží název asociace a popis a pod stejným názvem provede re-asociaci (to jsou slova).

Program

/*
 * Copyright (c) 2004 mentalis.org, MyXaml, Lukas Zapletal
 * All Rights Reserved
 * 
 * Licensed under the terms of the GNU General Public License
 * http://www.gnu.org/licenses/licenses.html#GPL
*/
 
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Microsoft.Win32;
using System.Security;
using DotNetScriptEngine;
 
namespace DotNetScriptEngine
{
    /// <summary>List of commands.</summary>
    internal struct CommandList
    {
        /// <summary>
        /// Holds the names of the commands.
        /// </summary>
        public ArrayList Captions;
        /// <summary>
        /// Holds the commands.
        /// </summary>
        public ArrayList Commands;
    }
    /// <summary>Properties of the file association.</summary>
    internal struct FileType
    {
        /// <summary>
        /// Holds the command names and the commands.
        /// </summary>
        public CommandList Commands;
        /// <summary>
        /// Holds the extension of the file type.
        /// </summary>
        public string Extension;
        /// <summary>
        /// Holds the proper name of the file type.
        /// </summary>
        public string ProperName;
        /// <summary>
        /// Holds the full name of the file type.
        /// </summary>
        public string FullName;
        /// <summary>
        /// Holds the name of the content type of the file type.
        /// </summary>
        public string ContentType;
        /// <summary>
        /// Holds the path to the resource with the icon of this file type.
        /// </summary>
        public string IconPath;
        /// <summary>
        /// Holds the icon index in the resource file.
        /// </summary>
        public short IconIndex;
    }
    /// <summary>Creates file associations for your programs.</summary>
    /// <example>The following example creates a file association for the type XYZ with a non-existent program.
    /// <br></br><br>VB.NET code</br>
    /// <pre>
    /// Dim FA as New FileAssociation
    /// FA.Extension = "xyz"
    /// FA.ContentType = "application/myprogram"
    /// FA.FullName = "My XYZ Files!"
    /// FA.ProperName = "XYZ File"
    /// FA.AddCommand("open", "C:\mydir\myprog.exe %1")
    /// FA.Create
    /// </pre>
    /// <br>C# code</br>
    /// <pre>
    /// FileAssociation FA = new FileAssociation();
    /// FA.Extension = "xyz";
    /// FA.ContentType = "application/myprogram";
    /// FA.FullName = "My XYZ Files!";
    /// FA.ProperName = "XYZ File";
    /// FA.AddCommand("open", "C:\\mydir\\myprog.exe %1");
    /// FA.Create();
    /// </pre>
    /// </example>
    public class FileAssociation
    {
        /// <summary>Initializes an instance of the FileAssociation class.</summary>
        public FileAssociation()
        {
            FileInfo = new FileType();
            FileInfo.Commands.Captions = new ArrayList();
            FileInfo.Commands.Commands = new ArrayList();
        }
        /// <summary>Gets or sets the proper name of the file type.</summary>
        /// <value>A String representing the proper name of the file type.</value>
        public string ProperName
        {
            get
            {
                return FileInfo.ProperName;
            }
            set
            {
                FileInfo.ProperName = value;
            }
        }
        /// <summary>Gets or sets the full name of the file type.</summary>
        /// <value>A String representing the full name of the file type.</value>
        public string FullName
        {
            get
            {
                return FileInfo.FullName;
            }
            set
            {
                FileInfo.FullName = value;
            }
        }
        /// <summary>Gets or sets the content type of the file type.</summary>
        /// <value>A String representing the content type of the file type.</value>
        public string ContentType
        {
            get
            {
                return FileInfo.ContentType;
            }
            set
            {
                FileInfo.ContentType = value;
            }
        }
        /// <summary>Gets or sets the extension of the file type.</summary>
        /// <value>A String representing the extension of the file type.</value>
        /// <remarks>If the extension doesn't start with a dot ("."), a dot is automatically added.</remarks>
        public string Extension
        {
            get
            {
                return FileInfo.Extension;
            }
            set
            {
                if (value.Substring(0, 1) != ".")
                    value = "." + value;
                FileInfo.Extension = value;
            }
        }
        /// <summary>Gets or sets the index of the icon of the file type.</summary>
        /// <value>A short representing the index of the icon of the file type.</value>
        public short IconIndex
        {
            get
            {
                return FileInfo.IconIndex;
            }
            set
            {
                FileInfo.IconIndex = value;
            }
        }
        /// <summary>Gets or sets the path of the resource that contains the icon for the file type.</summary>
        /// <value>A String representing the path of the resource that contains the icon for the file type.</value>
        /// <remarks>This resource can be an executable or a DLL.</remarks>
        public string IconPath
        {
            get
            {
                return FileInfo.IconPath;
            }
            set
            {
                FileInfo.IconPath = value;
            }
        }
        /// <summary>Adds a new command to the command list.</summary>
        /// <param name="Caption">The name of the command.</param>
        /// <param name="Command">The command to execute.</param>
        /// <exceptions cref="ArgumentNullException">Caption -or- Command is null (VB.NET: Nothing).</exceptions>
        public void AddCommand(string Caption, string Command)
        {
            if (Caption == null || Command == null)
                throw new ArgumentNullException();
            FileInfo.Commands.Captions.Add(Caption);
            FileInfo.Commands.Commands.Add(Command);
        }
 
        public bool Exists()
        {
            RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(Extension);
            object obj = RegKey.GetValue("");
            return obj != null;
        }
 
        public string CurrentProperName()
        {
            if (Extension == null)
                throw new ArgumentException("Property is not set");
            RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(Extension);
            return (string)RegKey.GetValue("");
        }
 
        public string CurrentContentType()
        {
            if (Extension == null)
                throw new ArgumentException("Property is not set");
            RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(Extension);
            return (string)RegKey.GetValue("Content Type");
        }
 
        public string CurrentFullName()
        {
            if (ProperName == null)
                throw new ArgumentException("Property is not set");
            RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(ProperName);
            return (string)RegKey.GetValue("");
        }
 
        /// <summary>Creates the file association.</summary>
        /// <exceptions cref="ArgumentNullException">Extension -or- ProperName is null (VB.NET: Nothing).</exceptions>
        /// <exceptions cref="ArgumentException">Extension -or- ProperName is empty.</exceptions>
        /// <exceptions cref="SecurityException">The user does not have registry write access.</exceptions>
        public void Create()
        {
            // remove the extension to avoid incompatibilities [such as DDE links]
            try
            {
                Remove();
            }
            catch (ArgumentException) { } // the extension doesn't exist
 
            // create the exception
            if (Extension == "" || ProperName == "")
                throw new ArgumentException();
            int cnt;
 
            try
            {
                RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(Extension);
                RegKey.SetValue("", ProperName);
 
                if (ContentType != null && ContentType != "")
                    RegKey.SetValue("Content Type", ContentType);
 
                RegKey.Close();
                RegKey = Registry.ClassesRoot.CreateSubKey(ProperName);
                RegKey.SetValue("", FullName);
                RegKey.Close();
 
                if (IconPath != "")
                {
                    RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "DefaultIcon");
                    RegKey.SetValue("", IconPath + "," + IconIndex.ToString());
                    RegKey.Close();
                }
 
                for (cnt = 0; cnt < FileInfo.Commands.Captions.Count; cnt++)
                {
                    RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "Shell" + "\\" + (String)FileInfo.Commands.Captions[cnt]);
                    RegKey = RegKey.CreateSubKey("Command");
                    RegKey.SetValue("", FileInfo.Commands.Commands[cnt]);
                    RegKey.Close();
                }
            }
            catch
            {
                throw new SecurityException();
            }
        }
        /// <summary>Removes the file association.</summary>
        /// <exceptions cref="ArgumentNullException">Extension -or- ProperName is null (VB.NET: Nothing).</exceptions>
        /// <exceptions cref="ArgumentException">Extension -or- ProperName is empty -or- the specified extension doesn't exist.</exceptions>
        /// <exceptions cref="SecurityException">The user does not have registry delete access.</exceptions>
        public void Remove()
        {
            if (Extension == null || ProperName == null)
                throw new ArgumentNullException();
            if (Extension == "" || ProperName == "")
                throw new ArgumentException();
            Registry.ClassesRoot.DeleteSubKeyTree(Extension);
            Registry.ClassesRoot.DeleteSubKeyTree(ProperName);
        }
        /// <summary>Holds the properties of the file type.</summary>
        private FileType FileInfo;
 
        public override string ToString()
        {
            return Extension + ", " + ProperName + ", " + FullName;
        }
    }
}
 
namespace FixAnnoyingOfficeDDE
{
    class Program
    {
        static void Register(string ext, string prg)
        {
            FileAssociation FA = new FileAssociation();
            FA.Extension = ext;
            if (FA.Exists())
            {
                FA.ProperName = FA.CurrentProperName();
                FA.ContentType = FA.CurrentContentType();
                FA.FullName = FA.CurrentFullName();
                System.Console.WriteLine("Registering " + FA);
                System.Console.WriteLine(" as " + prg);
                FA.AddCommand("open", prg);
                // delete existing association (incl. DDE stuff) and create brand new one
                FA.Create();
            }
            else
            {
                System.Console.WriteLine("The association " + FA.Extension + " did not exist, skipping");
            }
        }
 
        static void Main(string[] args)
        {
            //Register("txt", @"""c:\Program Files\Vim\vim71\gvim.exe"" ""%1""");
            Register("doc", @"""C:\Program Files\Microsoft Office\Office12\WINWORD.EXE"" /n ""%1""");
            Register("docx", @"""C:\Program Files\Microsoft Office\Office12\WINWORD.EXE"" /n ""%1""");
            Register("dot", @"""C:\Program Files\Microsoft Office\Office12\WINWORD.EXE"" /n ""%1""");
            Register("xls", @"""C:\Program Files\Microsoft Office\Office12\EXCEL.EXE"" /e ""%1""");
            Register("xlsx", @"""C:\Program Files\Microsoft Office\Office12\EXCEL.EXE"" /e ""%1""");
            Register("xlt", @"""C:\Program Files\Microsoft Office\Office12\EXCEL.EXE"" /e ""%1""");
            Register("ppt", @"""C:\Program Files\Microsoft Office\Office12\POWERPNT.EXE"" ""%1""");
            Register("pptx", @"""C:\Program Files\Microsoft Office\Office12\POWERPNT.EXE"" ""%1""");
        }
    }
}

Můžete si také stáhnout ZIP soubor s binárkou, zdrojákem a build baťákem.

Diskuze

If you can't read the letters on the image, download this .wav file to get them read to you.
pridej.cz
blog/reseni_vecneho_preregistrovavani_dde.txt · Poslední úprava: 29.05.2008 09:04 (external edit)
Creative Commons License Valid CSS Driven by DokuWiki Recent changes RSS feed Valid XHTML 1.0