zoukankan      html  css  js  c++  java
  • C#:关联程序和文件

     一、关联代码

            /// <summary>
            /// 关联程序和类型
            /// </summary>
            private void RegFileExt()
            {
                try
                {
                    string boardExe = @"BlackBoardToolinTeacher.UCBook.BlackBord.exe";
                    string boardExeFullName = Path.Combine(GlobalInfos.ExePath, boardExe);
                    if (File.Exists(boardExeFullName))
                    {
                        string MyExtName = ".dbb";
                        string MyType = "dbb_auto_file";
                        string MyContent = "application/dbb";
                        string command = """ + boardExeFullName + """ + " "%1"";
                        RegistryKey key = Registry.ClassesRoot.OpenSubKey(MyType);
                        if (key == null)
                        {
                            RegistryKey MyReg = Registry.ClassesRoot.CreateSubKey(MyExtName);
                            MyReg.SetValue("", MyType);
                            MyReg.SetValue("Content Type", MyContent);
                            MyReg = Registry.ClassesRoot.CreateSubKey(MyType);
                            MyReg.SetValue("", MyType);
                            MyReg = MyReg.CreateSubKey("Shell\Open\Command");
                            MyReg.SetValue("", command);
                            MyReg.Close();
                            _logger.Info("设置文件关联操作成功!");
                        }
                        else 
                        {
                            var myReg = key.OpenSubKey("Shell\Open\Command",true);
                            if (myReg!=null && (myReg.GetValue("")==null || myReg.GetValue("").ToString()!=command))
                            {
                                myReg.SetValue("", command);//解决因目录变化导致 注册表失效的问题
                                myReg.Close();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.ErrorFormat("设置文件关联失败!{0}",ex.Message);
                }
            }
            
    View Code

     二、注释

      1、前面是路径,后面的 %1就是可变参数,代表任意用xxxx.exe来执行的程序  (可传至于exe程序)

      2、string command = Environment.CommandLine.Trim();//获取进程命令行参数 含:.exe 全路径、打开文件的全路径(直接通过运行环境获取命令行)

      3、static void Main(string[] args){} //args:仅含打开文件的全路径,不包含.exe 全路径 (程序运行时获取参数)

    三、参考:

     1、

    1.写注册表的时候,最后路径要加上参数,如:
    
    [HKEY_CLASSES_ROOTSimushellopencommand]
    @=""x:\simu\Simulink.exe" "%1""
    
    前面是路径,后面的 %1就是参数了
    
    2.程序的主入口加入参数,以C#为例:
    
     static void Main(string[] args)
            {
                if (args.Length > 0)
                {
                    MessageBox.Show(args[0].ToString());
                }            
            }
    
    3.在写链接的时候自定义参数,程序会把整个链接都获取到,自己在进行解析 :
    
    比如:
    
    <a href="MyOfficeCon://hello">123123</a>
    
    会获取到
    
    “MyOfficeCon://hello”
    View Code


     2、线程开启程序时传参

    新建板书一.dbb
    
    E:workingClientBuildTeacher.Builduser16
    esourcembook_de0df31a6fd14e3390a8d65c19342801
    esource4d4db1cc.dbb
    
    _boardPath:  E:workingClientBuildTeacher.BuildinTeacher.UCBook.BlackBord.exe
    
    _args:  16 -p "E:workingClientBuildTeacher.Builduser16
    esourcembook_de0df31a6fd14e3390a8d65c19342801
    esource4d4db1cc.dbb" 新建板书一.dbb 
    
    System.Diagnostics.Process.Start(_boardPath, _args);
    View Code
  • 相关阅读:
    《人月神话》阅读笔记01
    第二次团队冲刺第八天
    第二次团队冲刺第七天
    第二次团队冲刺第六天
    找水王续
    团队第二次冲刺第五天
    第二次团队冲刺第四天
    原码, 反码, 补码 详解
    《用户故事与敏捷方法》阅读笔记06
    《用户故事与敏捷方法》阅读笔记05
  • 原文地址:https://www.cnblogs.com/shenchao/p/5492777.html
Copyright © 2011-2022 走看看