我将Associate * .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
我运行此代码 - 并且没有任何事情发生!!
我能做什么?
提前致谢
答案 0 :(得分:0)
首先尝试删除密钥,然后自行编写。