-
Notifications
You must be signed in to change notification settings - Fork 3
/
FileAss.cs
69 lines (63 loc) · 2.52 KB
/
FileAss.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.IO;
using System.Diagnostics;
using System.Runtime;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
namespace KMZRebuilder
{
public class FileAss
{
public static void SetFileAssociation(string Extension, string Class, string Command, string ExePath)
{
try
{
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Classes\\", true)
.CreateSubKey("." + Extension)
.CreateSubKey("OpenWithList")
.CreateSubKey(Path.GetFileName(ExePath))
.SetValue("", "\"" + ExePath + "\"" + " \"%1\"");
using (RegistryKey User_Classes = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Classes\\", true))
using (RegistryKey User_Ext = User_Classes.CreateSubKey("." + Extension))
using (RegistryKey User_FileClass = User_Classes.CreateSubKey(Class))
using (RegistryKey User_Command = User_FileClass.CreateSubKey("shell").CreateSubKey(Command).CreateSubKey("command"))
{
User_Ext.SetValue("", Class, RegistryValueKind.String);
User_Classes.SetValue("", Class, RegistryValueKind.String);
User_Command.SetValue("", "\"" + ExePath + "\"" + " \"%1\"");
};
}
catch
{
//Your code here
}
}
public static void SetFileOpenWith(string Extension, string ExePath)
{
try
{
Registry.CurrentUser.OpenSubKey("SOFTWARE\\Classes\\", true)
.CreateSubKey("." + Extension)
.CreateSubKey("OpenWithList")
.CreateSubKey(Path.GetFileName(ExePath))
.SetValue("", "\"" + ExePath + "\"" + " \"%1\"");
}
catch (Exception excpt)
{
//Your code here
}
}
public static void UpdateExplorer()
{
try
{
SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
}
catch { };
}
[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
}
}