一、關聯代碼

/// <summary> /// 關聯程序和類型 /// </summary> private void RegFileExt() { try { string boardExe = @"BlackBoardTool\bin\Teacher.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); } }
二、注釋
1、前面是路徑,后面的 %1就是可變參數,代表任意用xxxx.exe來執行的程序 (可傳至於exe程序)
2、string command = Environment.CommandLine.Trim();//獲取進程命令行參數 含:.exe 全路徑、打開文件的全路徑(直接通過運行環境獲取命令行)
3、static void Main(string[] args){} //args:僅含打開文件的全路徑,不包含.exe 全路徑 (程序運行時獲取參數)
三、參考:
1、

1.寫注冊表的時候,最后路徑要加上參數,如: [HKEY_CLASSES_ROOT\Simu\shell\open\command] @="\"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”
2、線程開啟程序時傳參

新建板書一.dbb E:\working\Client\Build\Teacher.Build\user\16\resource\mbook_de0df31a6fd14e3390a8d65c19342801\resource\4d4db1cc.dbb _boardPath: E:\working\Client\Build\Teacher.Build\bin\Teacher.UCBook.BlackBord.exe _args: 16 -p "E:\working\Client\Build\Teacher.Build\user\16\resource\mbook_de0df31a6fd14e3390a8d65c19342801\resource\4d4db1cc.dbb" 新建板書一.dbb System.Diagnostics.Process.Start(_boardPath, _args);