3 using System.Collections;
6 namespace TriStateTreeViewDemo
8 /// <summary>List of commands.</summary>
9 internal struct CommandList
12 /// Holds the names of the commands.
14 public ArrayList Captions;
16 /// Holds the commands.
18 public ArrayList Commands;
20 /// <summary>Properties of the file association.</summary>
21 internal struct FileType
24 /// Holds the command names and the commands.
26 public CommandList Commands;
28 /// Holds the extension of the file type.
30 public string Extension;
32 /// Holds the proper name of the file type.
34 public string ProperName;
36 /// Holds the full name of the file type.
38 public string FullName;
40 /// Holds the name of the content type of the file type.
42 public string ContentType;
44 /// Holds the path to the resource with the icon of this file type.
46 public string IconPath;
48 /// Holds the icon index in the resource file.
50 public short IconIndex;
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>
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")
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");
75 public class FileAssociation
77 /// <summary>Initializes an instance of the FileAssociation class.</summary>
78 public FileAssociation()
80 FileInfo = new FileType();
81 FileInfo.Commands.Captions = new ArrayList();
82 FileInfo.Commands.Commands = new ArrayList();
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
90 return FileInfo.ProperName;
94 FileInfo.ProperName = value;
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
103 return FileInfo.FullName;
107 FileInfo.FullName = value;
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
116 return FileInfo.ContentType;
120 FileInfo.ContentType = value;
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
130 return FileInfo.Extension;
134 if (value.Substring(0, 1) != ".")
136 FileInfo.Extension = value;
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
145 return FileInfo.IconIndex;
149 FileInfo.IconIndex = value;
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
159 return FileInfo.IconPath;
163 FileInfo.IconPath = value;
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)
172 if (Caption == null || Command == null)
173 throw new ArgumentNullException();
174 FileInfo.Commands.Captions.Add(Caption);
175 FileInfo.Commands.Commands.Add(Command);
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>
183 // remove the extension to avoid incompatibilities [such as DDE links]
188 catch (ArgumentException) {} // the extension doesn't exist
190 // create the exception
191 if (Extension == "" || ProperName == "")
192 throw new ArgumentException();
197 RegistryKey RegKey = Registry.ClassesRoot.CreateSubKey(Extension);
198 RegKey.SetValue("", ProperName);
200 if (ContentType != null && ContentType != "")
201 RegKey.SetValue("Content Type", ContentType);
204 RegKey = Registry.ClassesRoot.CreateSubKey(ProperName);
205 RegKey.SetValue("", FullName);
210 RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "DefaultIcon");
211 RegKey.SetValue("", IconPath + "," + IconIndex.ToString());
215 for (cnt = 0; cnt < FileInfo.Commands.Captions.Count; cnt++)
217 RegKey = Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "Shell" + "\\" + (String)FileInfo.Commands.Captions[cnt]);
218 RegKey = RegKey.CreateSubKey("Command");
219 RegKey.SetValue("", FileInfo.Commands.Commands[cnt]);
225 throw new SecurityException();
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>
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);
241 /// <summary>Holds the properties of the file type.</summary>
242 private FileType FileInfo;