WinForm 中自定义文件与自己的应用程序相关联

需求很简单:双击我的一个自定义扩展名的文件,打开相应的应用程序进行处理.查了一些资料,终于把它弄出来,在这里记录一下,以备查用.

①用到"Microsoft.Win32"命名空间.

using  Microsoft.Win32;

using Microsoft.Win32;

 

② 检查一下文件类型是否已被注册.

public   static   bool  FileTypeRegistered( string  extension)
{
            RegistryKey sluKey 
=  Registry.ClassesRoot.OpenSubKey(extension);
            
if  (sluKey  !=   null )
                
return   true ;
            
return   false ;
}

 ③ 删除已被注册的键值

public   static   void  UnRegistFileType( string  extension)
{
            
if  (FileTypeRegistered(extension))
            {
                Registry.ClassesRoot.DeleteSubKey(extension); 
                
string  relationName  =  extension.Substring( 1 , extension.Length  -   1 ).ToUpper()  +   "  FileType " ;
                Registry.ClassesRoot.DeleteSubKeyTree(relationName);
                Registry.ClassesRoot.Close();
            }
}

 ④ 注册自定义文件,并与自己的应用程序相关联

public   static   void  RegistFileType( string  extension)
{
            UnRegistFileType(extension);
            
string  relationName  =  extension.Substring( 1 , extension.Length  -   1 ).ToUpper()  +   "  FileType " ;
            RegistryKey sluKey 
=  Registry.ClassesRoot.CreateSubKey(extension);
            sluKey.SetValue(
"" , relationName);
            sluKey.Close();

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

            RegistryKey iconKey 
=  relationKey.CreateSubKey( " DefaultIcon " );//图标
            iconKey.SetValue(
"" , System.Windows.Forms.Application.StartupPath  +   @" \mainICO.ico " );

            RegistryKey shellKey 
=  relationKey.CreateSubKey( " Shell " );
            RegistryKey openKey 
=  shellKey.CreateSubKey( " Open " );
            RegistryKey commandKey 
=  openKey.CreateSubKey( " Command " );
            commandKey.SetValue(
"" , System.Windows.Forms.Application.ExecutablePath  +   "  %1 " );//是数字"1",双击文件之后就把文件的路径传递过来了.
            relationKey.Close();
}

 ⑤ 修改默认Main()函数,使之能接收参数

[STAThread]
static   void  Main( string [] args)
{
            
if  (args  !=   null   &&  args.Length  >   0 )
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
false );
                Application.Run(
new  MainForm(args[ 0 ]));
            }
            
else
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
false );
                Application.Run(
new  MainForm());
            }
}

 ⑥在主窗体构造函数中实现自己的业务逻辑...

 

不知道大家有没有更好的解决方案,一起学习一下.

转载于:https://www.cnblogs.com/donghaiyiyu/archive/2008/08/11/1265449.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值