我的程序中关联文件类型的问题
我有这段代码用于将 *.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试先删除密钥,然后编写自己的密钥。
Try deleting the keys first then writing your own.