[RTL]
[reactos.git] / reactos / tools / sysgen / RosBuilder / Util / FileAssociation.cs
1 using System;
2 using System.Security;
3 using System.Collections;
4 using Microsoft.Win32;
5
6 namespace TriStateTreeViewDemo
7 {
8 /// <summary>List of commands.</summary>
9 internal struct CommandList
10 {
11 /// <summary>
12 /// Holds the names of the commands.
13 /// </summary>
14 public ArrayList Captions;
15 /// <summary>
16 /// Holds the commands.
17 /// </summary>
18 public ArrayList Commands;
19 }
20 /// <summary>Properties of the file association.</summary>
21 internal struct FileType
22 {
23 /// <summary>
24 /// Holds the command names and the commands.
25 /// </summary>
26 public CommandList Commands;
27 /// <summary>
28 /// Holds the extension of the file type.
29 /// </summary>
30 public string Extension;
31 /// <summary>
32 /// Holds the proper name of the file type.
33 /// </summary>
34 public string ProperName;
35 /// <summary>
36 /// Holds the full name of the file type.
37 /// </summary>
38 public string FullName;
39 /// <summary>
40 /// Holds the name of the content type of the file type.
41 /// </summary>
42 public string ContentType;
43 /// <summary>
44 /// Holds the path to the resource with the icon of this file type.
45 /// </summary>
46 public string IconPath;
47 /// <summary>
48 /// Holds the icon index in the resource file.
49 /// </summary>
50 public short IconIndex;
51 }
52 /// <summary>Creates file associations for your programs.</summary>
53 /// <example>The following example creates a file association for the type XYZ with a non-existent program.
54 /// <br></br><br>VB.NET code</br>
55 /// <code>
56 /// Dim FA as New FileAssociation
57 /// FA.Extension = "xyz"
58 /// FA.ContentType = "application/myprogram"
59 /// FA.FullName = "My XYZ Files!"
60 /// FA.ProperName = "XYZ File"
61 /// FA.AddCommand("open", "C:\mydir\myprog.exe %1")
62 /// FA.Create
63 /// </code>
64 /// <br>C# code</br>
65 /// <code>
66 /// FileAssociation FA = new FileAssociation();
67 /// FA.Extension = "xyz";
68 /// FA.ContentType = "application/myprogram";
69 /// FA.FullName = "My XYZ Files!";
70 /// FA.ProperName = "XYZ File";
71 /// FA.AddCommand("open", "C:\\mydir\\myprog.exe %1");
72 /// FA.Create();
73 /// </code>
74 /// </example>
75 public class FileAssociation
76 {
77 /// <summary>Initializes an instance of the FileAssociation class.</summary>
78 public FileAssociation()
79 {
80 FileInfo = new FileType();
81 FileInfo.Commands.Captions = new ArrayList();
82 FileInfo.Commands.Commands = new ArrayList();
83 }
84 /// <summary>Gets or sets the proper name of the file type.</summary>
85 /// <value>A String representing the proper name of the file type.</value>
86 public string ProperName
87 {
88 get
89 {
90 return FileInfo.ProperName;
91 }
92 set
93 {
94 FileInfo.ProperName = value;
95 }
96 }
97 /// <summary>Gets or sets the full name of the file type.</summary>
98 /// <value>A String representing the full name of the file type.</value>
99 public string FullName
100 {
101 get
102 {
103 return FileInfo.FullName;
104 }
105 set
106 {
107 FileInfo.FullName = value;
108 }
109 }
110 /// <summary>Gets or sets the content type of the file type.</summary>
111 /// <value>A String representing the content type of the file type.</value>
112 public string ContentType
113 {
114 get
115 {
116 return FileInfo.ContentType;
117 }
118 set
119 {
120 FileInfo.ContentType = value;
121 }
122 }
123 /// <summary>Gets or sets the extension of the file type.</summary>
124 /// <value>A String representing the extension of the file type.</value>
125 /// <remarks>If the extension doesn't start with a dot ("."), a dot is automatically added.</remarks>
126 public string Extension
127 {
128 get
129 {
130 return FileInfo.Extension;
131 }
132 set
133 {
134 if (value.Substring(0, 1) != ".")
135 value = "." + value;
136 FileInfo.Extension = value;
137 }
138 }
139 /// <summary>Gets or sets the index of the icon of the file type.</summary>
140 /// <value>A short representing the index of the icon of the file type.</value>
141 public short IconIndex
142 {
143 get
144 {
145 return FileInfo.IconIndex;
146 }
147 set
148 {
149 FileInfo.IconIndex = value;
150 }
151 }
152 /// <summary>Gets or sets the path of the resource that contains the icon for the file type.</summary>
153 /// <value>A String representing the path of the resource that contains the icon for the file type.</value>
154 /// <remarks>This resource can be an executable or a DLL.</remarks>
155 public string IconPath
156 {
157 get
158 {
159 return FileInfo.IconPath;
160 }
161 set
162 {
163 FileInfo.IconPath = value;
164 }
165 }
166 /// <summary>Adds a new command to the command list.</summary>
167 /// <param name="Caption">The name of the command.</param>
168 /// <param name="Command">The command to execute.</param>
169 /// <exceptions cref="ArgumentNullException">Caption -or- Command is null (VB.NET: Nothing).</exceptions>
170 public void AddCommand(string Caption, string Command)
171 {
172 if (Caption == null || Command == null)
173 throw new ArgumentNullException();
174 FileInfo.Commands.Captions.Add(Caption);
175 FileInfo.Commands.Commands.Add(Command);
176 }
177 /// <summary>Creates the file association.</summary>
178 /// <exceptions cref="ArgumentNullException">Extension -or- ProperName is null (VB.NET: Nothing).</exceptions>
179 /// <exceptions cref="ArgumentException">Extension -or- ProperName is empty.</exceptions>
180 /// <exceptions cref="SecurityException">The user does not have registry write access.</exceptions>
181 public void Create()
182 {
183 // remove the extension to avoid incompatibilities [such as DDE links]
184 try
185 {
186 Remove();
187 }
188 catch (ArgumentException) {} // the extension doesn't exist
189
190 // create the exception
191 if (Extension == "" || ProperName == "")
192 throw new ArgumentException();
193 int cnt;
194
195 try
196 {
197 RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(Extension);
198 RegKey.SetValue("", ProperName);
199
200 if (ContentType != null && ContentType != "")
201 RegKey.SetValue("Content Type", ContentType);
202
203 RegKey.Close();
204 RegKey = Registry.ClassesRoot.CreateSubKey(ProperName);
205 RegKey.SetValue("", FullName);
206 RegKey.Close();
207
208 if (IconPath != "")
209 {
210 RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "DefaultIcon");
211 RegKey.SetValue("", IconPath + "," + IconIndex.ToString());
212 RegKey.Close();
213 }
214
215 for (cnt = 0; cnt < FileInfo.Commands.Captions.Count; cnt++)
216 {
217 RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "Shell" + "\\" + (String)FileInfo.Commands.Captions[cnt]);
218 RegKey = RegKey.CreateSubKey("Command");
219 RegKey.SetValue("", FileInfo.Commands.Commands[cnt]);
220 RegKey.Close();
221 }
222 }
223 catch
224 {
225 throw new SecurityException();
226 }
227 }
228 /// <summary>Removes the file association.</summary>
229 /// <exceptions cref="ArgumentNullException">Extension -or- ProperName is null (VB.NET: Nothing).</exceptions>
230 /// <exceptions cref="ArgumentException">Extension -or- ProperName is empty -or- the specified extension doesn't exist.</exceptions>
231 /// <exceptions cref="SecurityException">The user does not have registry delete access.</exceptions>
232 public void Remove()
233 {
234 if (Extension == null || ProperName == null)
235 throw new ArgumentNullException();
236 if (Extension == "" || ProperName == "")
237 throw new ArgumentException();
238 Registry.ClassesRoot.DeleteSubKeyTree(Extension);
239 Registry.ClassesRoot.DeleteSubKeyTree(ProperName);
240 }
241 /// <summary>Holds the properties of the file type.</summary>
242 private FileType FileInfo;
243 }
244 }