using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; using System.Runtime.InteropServices; namespace Xplayer { public class LinkFile { #region 程序關(guān)聯(lián)到文件 [DllImport("shell32.dll")]//立即刷新顯示而不重啟電腦 public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2); /// <summary> /// 關(guān)聯(lián)文件 雙擊啟動(dòng)程序的文件 /// </summary> public static string[] FileNameWhoInvokeEXE = null; /// <summary> /// 設(shè)置關(guān)聯(lián)文件 /// </summary> /// <param name="ExName">文件擴(kuò)展名</param> /// <param name="TypeName">文件類型名</param> /// <param name="IconPath">文件圖標(biāo)</param> /// <param name="AppPath">啟動(dòng)程序</param> public static void SetLink(string ExName, string TypeName, string IconPath, string AppPath) { try { RegistryKey root = Registry.ClassesRoot;//root key //change extendname link root.CreateSubKey(ExName); root.DeleteSubKeyTree(ExName); root.CreateSubKey(ExName); RegistryKey ExKey = root.OpenSubKey(ExName, true); ExKey.SetValue("", TypeName); ExKey.SetValue("Content Type", TypeName);//add value //create new filetype root.CreateSubKey(TypeName); root.DeleteSubKeyTree(TypeName); root.CreateSubKey(TypeName); RegistryKey TypeKey = root.OpenSubKey(TypeName, true); TypeKey.SetValue("", TypeName); TypeKey.CreateSubKey("DefaultIcon").SetValue("", IconPath);//new subkey icon TypeKey.CreateSubKey("shell\\Open\\Command").SetValue("", "\"" + AppPath + "\"" + "\"" + " %1" + "\"");//new subkey command TypeKey.Close(); root.Close(); SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);//not need reboot } catch { } } /// <summary> /// 解除關(guān)聯(lián)文件 /// </summary> /// <param name="TypeName"></param> public static void DelLink(string TypeName) { try { RegistryKey root = Registry.ClassesRoot; root.DeleteSubKeyTree(TypeName);//delete type key root.Close(); SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);//not need reboot } catch { } } #endregion #region 程序加入系統(tǒng)右鍵 /// <summary> /// 程序關(guān)聯(lián)擴(kuò)展名文件右鍵菜單 /// </summary> /// <param name="ExName">擴(kuò)展名文件</param> /// <param name="MenuName">右鍵顯示名稱</param> /// <param name="AppPath">程序</param> public static void SetRightMenuLink(string MenuName, string AppPath) { try { RegistryKey shellKey = Registry.ClassesRoot.OpenSubKey("*\\shell", true); shellKey.CreateSubKey(MenuName).CreateSubKey("command").SetValue("", "\"" + AppPath + "\"" + "\"" + " %1" + "\""); shellKey.Close(); } catch { } } /// <summary> /// 解除程序關(guān)聯(lián)擴(kuò)展名文件右鍵菜單 /// </summary> /// <param name="ExName">擴(kuò)展名文件</param> public static void DeleteRightMenuLink(string MenuName) { try { RegistryKey fileshellKey = Registry.ClassesRoot.OpenSubKey("*\\shell", true); fileshellKey.DeleteSubKeyTree(MenuName); fileshellKey.Close(); } catch { } } #endregion #region 跨進(jìn)程通信 public const int WM_COPYDATA = 0x004A; [DllImport("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, ref COPYDATASTRUCT lParam); /// <summary> /// 所需傳遞的結(jié)構(gòu)體 /// </summary> public struct COPYDATASTRUCT { public IntPtr dwData;//用戶定義數(shù)據(jù) public int cbData;//數(shù)據(jù)大小 [MarshalAs(UnmanagedType.LPStr)] public string lpData;//指向數(shù)據(jù)的指針 } /// <summary> /// 發(fā)送字符串 /// </summary> /// <param name="hWnd"></param> /// <param name="str"></param> public static void SendStr(IntPtr hWnd, string str) { try { byte[] sarr = System.Text.Encoding.Default.GetBytes(str); int len = sarr.Length; COPYDATASTRUCT cds; cds.dwData = (IntPtr)100; cds.lpData = str; cds.cbData = len + 1; SendMessage(hWnd, WM_COPYDATA, 0, ref cds); } catch { } } //#region 跨進(jìn)程通信 接收消息 ///// <summary> ///// 重寫(xiě)接收函數(shù) 放到主窗體類中 ///// </summary> ///// <param name="m"></param> //protected override void DefWndProc(ref Message m) //{ // if (m.Msg == LinkFile.WM_COPYDATA) // { // LinkFile.COPYDATASTRUCT mystr = new LinkFile.COPYDATASTRUCT(); // Type mytype = mystr.GetType(); // mystr = (LinkFile.COPYDATASTRUCT)m.GetLParam(mytype);//獲取類型 // string str = mystr.lpData; // wmplayer1.URL = str; // listBox1.Items.Add(str); // listBox1.SelectedItems.Clear(); // listBox1.SetSelected(listBox1.Items.Count - 1, true); // labelFormTitle.Text = str; // } // base.DefWndProc(ref m); //} //#endregion #endregion } } |
聯(lián)系客服