我的程序中关联文件类型的问题

发布于 2024-11-03 12:00:34 字数 2503 浏览 0 评论 0原文

我有这段代码用于将 *.sdf 文件关联到我的 C# 程序:

 public class FileAssociation
        {
            // Associate file extension with progID, description, icon and application
            public static void Associate(string extension,
                   string progID, string description, string icon, string application)
            {
                Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);
                if (progID != null && progID.Length > 0)
                    using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
                    {
                        if (description != null)
                            key.SetValue("", description);
                        if (icon != null)
                            key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
                        if (application != null)
                            key.CreateSubKey(@"Shell\Open\Command").SetValue("",
                                        ToShortPathName(application) + " \"%1\"");
                    }
            }

            // Return true if extension already associated in registry
            public static bool IsAssociated(string extension)
            {
                return (Registry.ClassesRoot.OpenSubKey(extension, false) != null);
            }

            [DllImport("Kernel32.dll")]
            private static extern uint GetShortPathName(string lpszLongPath,
                [Out] StringBuilder lpszShortPath, uint cchBuffer);

            // Return short path format of a file name
            private static string ToShortPathName(string longName)
            {
                StringBuilder s = new StringBuilder(1000);
                uint iSize = (uint)s.Capacity;
                uint iRet = GetShortPathName(longName, s, iSize);
                return s.ToString();
            }
        }

我像这样使用它:

   if (!FileAssociation.IsAssociated(".sdf"))
                FileAssociation.Associate(".sdf", "ClassID.ProgID", "sdf File", @"d:\ICO.ico", @"D:\OpenSDF.exe");

我也尝试这个:

   if (FileAssociation.IsAssociated(".sdf"))
                FileAssociation.Associate(".sdf", "ClassID.ProgID", "sdf File", @"d:\ICO.ico", @"D:\OpenSDF.exe");

我的问题是当文件已经与另一个程序关联时它就无法工作!

例如:我的计算机上的 *.sdf 文件关联使用 Visual-studio 2008 打开,

我运行此代码 - 什么也没有发生!!

我能做什么?

提前致谢

i have this code for Associate *.sdf files to my C# program:

 public class FileAssociation
        {
            // Associate file extension with progID, description, icon and application
            public static void Associate(string extension,
                   string progID, string description, string icon, string application)
            {
                Registry.ClassesRoot.CreateSubKey(extension).SetValue("", progID);
                if (progID != null && progID.Length > 0)
                    using (RegistryKey key = Registry.ClassesRoot.CreateSubKey(progID))
                    {
                        if (description != null)
                            key.SetValue("", description);
                        if (icon != null)
                            key.CreateSubKey("DefaultIcon").SetValue("", ToShortPathName(icon));
                        if (application != null)
                            key.CreateSubKey(@"Shell\Open\Command").SetValue("",
                                        ToShortPathName(application) + " \"%1\"");
                    }
            }

            // Return true if extension already associated in registry
            public static bool IsAssociated(string extension)
            {
                return (Registry.ClassesRoot.OpenSubKey(extension, false) != null);
            }

            [DllImport("Kernel32.dll")]
            private static extern uint GetShortPathName(string lpszLongPath,
                [Out] StringBuilder lpszShortPath, uint cchBuffer);

            // Return short path format of a file name
            private static string ToShortPathName(string longName)
            {
                StringBuilder s = new StringBuilder(1000);
                uint iSize = (uint)s.Capacity;
                uint iRet = GetShortPathName(longName, s, iSize);
                return s.ToString();
            }
        }

and i use it like this:

   if (!FileAssociation.IsAssociated(".sdf"))
                FileAssociation.Associate(".sdf", "ClassID.ProgID", "sdf File", @"d:\ICO.ico", @"D:\OpenSDF.exe");

i try this too:

   if (FileAssociation.IsAssociated(".sdf"))
                FileAssociation.Associate(".sdf", "ClassID.ProgID", "sdf File", @"d:\ICO.ico", @"D:\OpenSDF.exe");

my problem is when the file Already associated with another program it wont work !

for example: the *.sdf files on my computer Associate to open with Visual-studio 2008

i run this code - and Nothing happens !!

what i can do ?

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渔村楼浪 2024-11-10 12:00:34

尝试先删除密钥,然后编写自己的密钥。

Try deleting the keys first then writing your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文