通过注册表实现文件关联
此程序演示的是如何关联.txt文件 运行演示后不要忘了删除掉关联哦
Code
1 using System;
2 using System.Windows;
3 using Microsoft.Win32;
4 using System.IO;
5
6 namespace FileAssociation
7 {
8 /// <summary>
9 /// Window1.xaml 的交互逻辑
10 /// </summary>
11 public partial class Window1 : Window
12 {
13 public Window1()
14 {
15 InitializeComponent();
16 }
17
18 /// <summary>
19 /// 设置文件关联
20 /// </summary>
21 /// <param name="p_Filename">程序的名称</param>
22 /// <param name="p_FileTypeName">扩展名</param>
23 public static void SaveReg(string Filename, string FileTypeName)
24 {
25 RegistryKey RegKey = Registry.ClassesRoot.OpenSubKey("", true);//打开注册表
26
27 RegistryKey VKey = RegKey.OpenSubKey(FileTypeName, true);
28 if (VKey != null) //判断注册表项是否存在
29 VKey.SetValue("", "Exec");
30 else
31 {
32 RegKey.CreateSubKey(FileTypeName);
33 VKey = RegKey.OpenSubKey(FileTypeName, true);
34 VKey.SetValue("", "Exec");
35 }
36
37 VKey = RegKey.OpenSubKey("Exec", true);
38 if (VKey != null)
39 RegKey.DeleteSubKeyTree("Exec"); //如果不等于空 则删除注册表
40
41 RegKey.CreateSubKey("Exec");
42 VKey = RegKey.OpenSubKey("Exec", true);
43 VKey.CreateSubKey("shell");
44 VKey = VKey.OpenSubKey("shell", true);//写入必须路径
45 VKey.CreateSubKey("open");
46 VKey = VKey.OpenSubKey("open", true);
47 VKey.CreateSubKey("command");
48 VKey = VKey.OpenSubKey("command", true);
49 string _PathString = "\"" + Filename + "\" \"%1\"";
50 VKey.SetValue("", _PathString); //写入数据
51
52
53 }
54 /// <summary>
55 /// 删除恢复文件关联
56 /// </summary>
57 /// <param name="p_FileTypeName">扩展名</param>
58 public static void ResReg(string FileTypeName)
59 {
60 RegistryKey Regkey = Registry.ClassesRoot.OpenSubKey("", true);
61 RegistryKey Vkey = Regkey.OpenSubKey(FileTypeName, true);
62 if (Vkey != null)
63 Vkey.SetValue("", "txtfile"); //恢复到默认的值
64 Regkey.DeleteSubKeyTree("Exec");
65
66 }
67
68 private void add_Click(object sender, RoutedEventArgs e)
69 {
70 string p_Filename = this.GetType().Assembly.Location;//获取该程序的完整路径
71 SaveReg(p_Filename, ".txt");
72 }
73
74 private void clear_Click(object sender, RoutedEventArgs e)
75 {
76 ResReg(".txt");
77 }
78
79 private void Window_Loaded(object sender, RoutedEventArgs e)
80 {
81 string PathFileName = Environment.CommandLine; //获取启动行命令
82 string[] para = PathFileName.Split('\"'); //进行分割并判断是否通过关联的文件打开程序
83 if (para.Length > 3)
84 {
85 ReadText(para[3]);
86 }
87 }
88 private void ReadText(string PathFileName)
89 {
90 StreamReader sr = new StreamReader(PathFileName);
91 string readtxt = sr.ReadToEnd();
92 textbox.Text = readtxt;
93 sr.Close();
94 }
95
96 }
97 }
1 using System;
2 using System.Windows;
3 using Microsoft.Win32;
4 using System.IO;
5
6 namespace FileAssociation
7 {
8 /// <summary>
9 /// Window1.xaml 的交互逻辑
10 /// </summary>
11 public partial class Window1 : Window
12 {
13 public Window1()
14 {
15 InitializeComponent();
16 }
17
18 /// <summary>
19 /// 设置文件关联
20 /// </summary>
21 /// <param name="p_Filename">程序的名称</param>
22 /// <param name="p_FileTypeName">扩展名</param>
23 public static void SaveReg(string Filename, string FileTypeName)
24 {
25 RegistryKey RegKey = Registry.ClassesRoot.OpenSubKey("", true);//打开注册表
26
27 RegistryKey VKey = RegKey.OpenSubKey(FileTypeName, true);
28 if (VKey != null) //判断注册表项是否存在
29 VKey.SetValue("", "Exec");
30 else
31 {
32 RegKey.CreateSubKey(FileTypeName);
33 VKey = RegKey.OpenSubKey(FileTypeName, true);
34 VKey.SetValue("", "Exec");
35 }
36
37 VKey = RegKey.OpenSubKey("Exec", true);
38 if (VKey != null)
39 RegKey.DeleteSubKeyTree("Exec"); //如果不等于空 则删除注册表
40
41 RegKey.CreateSubKey("Exec");
42 VKey = RegKey.OpenSubKey("Exec", true);
43 VKey.CreateSubKey("shell");
44 VKey = VKey.OpenSubKey("shell", true);//写入必须路径
45 VKey.CreateSubKey("open");
46 VKey = VKey.OpenSubKey("open", true);
47 VKey.CreateSubKey("command");
48 VKey = VKey.OpenSubKey("command", true);
49 string _PathString = "\"" + Filename + "\" \"%1\"";
50 VKey.SetValue("", _PathString); //写入数据
51
52
53 }
54 /// <summary>
55 /// 删除恢复文件关联
56 /// </summary>
57 /// <param name="p_FileTypeName">扩展名</param>
58 public static void ResReg(string FileTypeName)
59 {
60 RegistryKey Regkey = Registry.ClassesRoot.OpenSubKey("", true);
61 RegistryKey Vkey = Regkey.OpenSubKey(FileTypeName, true);
62 if (Vkey != null)
63 Vkey.SetValue("", "txtfile"); //恢复到默认的值
64 Regkey.DeleteSubKeyTree("Exec");
65
66 }
67
68 private void add_Click(object sender, RoutedEventArgs e)
69 {
70 string p_Filename = this.GetType().Assembly.Location;//获取该程序的完整路径
71 SaveReg(p_Filename, ".txt");
72 }
73
74 private void clear_Click(object sender, RoutedEventArgs e)
75 {
76 ResReg(".txt");
77 }
78
79 private void Window_Loaded(object sender, RoutedEventArgs e)
80 {
81 string PathFileName = Environment.CommandLine; //获取启动行命令
82 string[] para = PathFileName.Split('\"'); //进行分割并判断是否通过关联的文件打开程序
83 if (para.Length > 3)
84 {
85 ReadText(para[3]);
86 }
87 }
88 private void ReadText(string PathFileName)
89 {
90 StreamReader sr = new StreamReader(PathFileName);
91 string readtxt = sr.ReadToEnd();
92 textbox.Text = readtxt;
93 sr.Close();
94 }
95
96 }
97 }
最近打算做个媒体播放器 于是就研究了下怎么用C#实现程序与文件的关联 刚学习.net编程不久 希望对新手有一些帮助。。。