使用C#自动注册自定义文件类型

简介:

我们自己编写的应用中,经常会用自定义类型的文件的来保存与应用相关的数据,如何改变我们的自定义类型的文件的图标?如何双击自定义类型文件的时候启动相应的应用程序了?本文将告诉你如何通过程序来完成这些功能!

比如.xcf文件就是XCodeFactory应用程序的项目文件。如果没有向Windows注册表注册该文件类型,那么.xcf文件的图标将是windows的文件默认图标,并且你双击一个a.xcf文件,也不会自动启动XCodeFactory应用程序来加载a.xcf文件。如何使.xcf文件的图标变成我自己喜爱的图标、如何完成像点击.doc文件就自动打开word程序的功能,下面将告诉你解决方案。

我们可以通过手动修改注册表来完成上述任务,更好的方式是,通过程序来实现。这样,在安装应用程序时,就可以自动的注册自定义文件类型了。我通过FileTypeRegister静态类来完成这些功能。首先,将注册需要用到的信息封装成FileTypeRegInfo,定义如下:

public class FileTypeRegInfo
{
/// <summary>
/// 目标类型文件的扩展名
/// </summary>
public string ExtendName ; //".xcf"

/// <summary>
/// 目标文件类型说明
/// </summary>
public string Description ; //"XCodeFactory项目文件"

/// <summary>
/// 目标类型文件关联的图标
/// </summary>
public string IcoPath ;

/// <summary>
/// 打开目标类型文件的应用程序
/// </summary>
public string ExePath ;

public FileTypeRegInfo()
{
}

public FileTypeRegInfo(string extendName)
{
this.ExtendName = extendName ;
}
}

FileTypeRegister类主要是操作注册表中的内容,实现如下:

/// <summary>
/// FileTypeRegister 用于注册自定义的文件类型。
/// zhuweisky 2005.08.31
/// </summary>
public class FileTypeRegister
{
RegisterFileType#region RegisterFileType
/// <summary>
/// RegisterFileType 使文件类型与对应的图标及应用程序关联起来。
/// </summary>
public static void RegisterFileType(FileTypeRegInfo regInfo)
{
if(RegistryHelper.FileTypeRegistered(regInfo.ExtendName))
{
return ;
}

string relationName = regInfo.ExtendName.Substring(1 ,regInfo.ExtendName.Length-1).ToUpper() + "_FileType" ;

RegistryKey fileTypeKey = Registry.ClassesRoot.CreateSubKey(regInfo.ExtendName) ;
fileTypeKey.SetValue("" ,relationName) ;
fileTypeKey.Close() ;

RegistryKey relationKey = Registry.ClassesRoot.CreateSubKey(relationName) ;
relationKey.SetValue("" ,regInfo.Description) ;

RegistryKey iconKey = relationKey.CreateSubKey("DefaultIcon") ;
iconKey.SetValue("" ,regInfo.IcoPath) ;

RegistryKey shellKey = relationKey.CreateSubKey("Shell") ;
RegistryKey openKey = shellKey.CreateSubKey("Open") ;
RegistryKey commandKey = openKey.CreateSubKey("Command") ;
commandKey.SetValue("" ,regInfo.ExePath + " %1") ;

relationKey.Close() ;
}

/// <summary>
/// GetFileTypeRegInfo 得到指定文件类型关联信息
/// </summary>
public static FileTypeRegInfo GetFileTypeRegInfo(string extendName)
{
if(! RegistryHelper.FileTypeRegistered(extendName))
{
return null ;
}

FileTypeRegInfo regInfo = new FileTypeRegInfo(extendName) ;

string relationName = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName) ;
regInfo.Description = relationKey.GetValue("").ToString() ;

RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon") ;
regInfo.IcoPath = iconKey.GetValue("").ToString();

RegistryKey shellKey = relationKey.OpenSubKey("Shell") ;
RegistryKey openKey = shellKey.OpenSubKey("Open") ;
RegistryKey commandKey = openKey.OpenSubKey("Command") ;
string temp = commandKey.GetValue("").ToString() ;
regInfo.ExePath = temp.Substring(0 ,temp.Length-3) ;

return regInfo ;
}

/// <summary>
/// UpdateFileTypeRegInfo 更新指定文件类型关联信息
/// </summary>
public static bool UpdateFileTypeRegInfo(FileTypeRegInfo regInfo)
{
if(! RegistryHelper.FileTypeRegistered(regInfo.ExtendName))
{
return false ;
}
string extendName = regInfo.ExtendName ;
string relationName = extendName.Substring(1 ,extendName.Length-1).ToUpper() + "_FileType" ;
RegistryKey relationKey = Registry.ClassesRoot.OpenSubKey(relationName ,true) ;
relationKey.SetValue("" ,regInfo.Description) ;

RegistryKey iconKey = relationKey.OpenSubKey("DefaultIcon" ,true) ;
iconKey.SetValue("" ,regInfo.IcoPath);

RegistryKey shellKey = relationKey.OpenSubKey("Shell") ;
RegistryKey openKey = shellKey.OpenSubKey("Open") ;
RegistryKey commandKey = openKey.OpenSubKey("Command" ,true) ;
commandKey.SetValue("" ,regInfo.ExePath + " %1") ;

relationKey.Close() ;
return true ;
}

/// <summary>
/// FileTypeRegistered 指定文件类型是否已经注册
/// </summary>
public static bool FileTypeRegistered(string extendName)
{
RegistryKey softwareKey = Registry.ClassesRoot.OpenSubKey(extendName);
if(softwareKey != null)
{
return true ;
}

return false ;
}
#endregion
}

要注意的是commandKey.SetValue("" ,regInfo.ExePath + " %1") ;其中" %1"表示将被双击的文件的路径传给目标应用程序,这样在双击a.xcf文件时,XCodeFactory才知道要打开哪个文件,所以应用程序的Main方法要被改写为带有参数的形式,就像下面的样子:

[STAThread]
static void Main(string[] args)
{
if((args!= null) && (args.Length > 0))
{
string filePath = "" ;
for(int i=0 ;i<args.Length ;i++)
{
filePath += " " + args[i] ;
}

MainForm.XcfFilePath = filePath.Trim() ;
}

Application.Run(new MainForm());
} 




本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2010/02/22/1670944.html,如需转载请自行联系原作者  

相关文章
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
262 0
|
C# C++
C# 自定义时间进度条
本文作者通过参考leslie_xin的一篇文章,成功创建了一个自定义的WinForms控件——时间进度条,该控件带有时间刻度和多种可定制的属性,如颜色、时间间隔等。作者在控件中加入了开始和结束时间,以及自适应的时间刻度间隔。控件能根据设置显示时间标签,并提供了事件处理,如值改变时的触发。代码中包含了计算时间刻度、绘制刻度线和时间标签的逻辑。作者强调了避免循环调用事件、使用OnXXX()形式的事件处理函数以及注意自定义控件中的属性和事件设计。
533 7
C#或Winform中的消息通知之自定义优雅漂亮的通知效果
Custom Notification自定义通知提示,一款非常优雅漂亮的自定义通知效果,主要介绍其实现思路、调整和优化...
2077 0
C#或Winform中的消息通知之自定义优雅漂亮的通知效果
|
编译器 C#
c# 自定义扩展方法
c# 自定义扩展方法
|
开发框架 .NET 编译器
总结一下 C# 如何自定义特性 Attribute 并进行应用
总结一下 C# 如何自定义特性 Attribute 并进行应用
670 1
|
编译器 C#
C#中导入其它自定义的命名空间
c#中怎么导入其它自定义的命名空间首先要确保已经导入了想要导入的自定义的命名空间。如上图这时编译器应该会报错,此时就需要手动去添加引用了,cs文件默认没有添加引用,只是加载了想要导入的命名空间,但是没有添加引用,所以需要自己要手动添加引用。切记!然后会有一个对话框选择你想引用的命名空间,点击确定即可。注意:一般而言,C#中如果没有改变那么一般项目的类名都默认是Program,在引用时需要注...
335 1
C#中导入其它自定义的命名空间
|
移动开发 JavaScript 安全
C# 实现微信自定义分享
C# 实现微信自定义分享
|
存储 设计模式 缓存
C# 实现 key-value 结构自定义缓存 CustomCache
C# 实现 key-value 结构自定义缓存 CustomCache
454 1
C# 实现 key-value 结构自定义缓存 CustomCache
C#学习相关系列之自定义遍历器
C#学习相关系列之自定义遍历器
270 0
|
C# 图形学
C#绘制自定义小人
C#绘制自定义小人