using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Collections;
using Microsoft.Win32;
namespace QC.QTMApplication
{
internal struct CommandArrayList
{
public ArrayList Captions;
public ArrayList Commands;
}
///
internal struct FileType
{
public CommandArrayList Commands;
public string Extension;
public string ProperName;
public string FullName;
public string ContentType;
public string IconPath;
public short IconIndex;
}
///
///
///
VB.NET code
///
/// Dim FA as New FileAssociation
/// FA.Extension = "xyz"
/// FA.ContentType = "application/myprogram"
/// FA.FullName = "My XYZ Files!"
/// FA.ProperName = "XYZ File"
/// FA.AddCommand("open", "C:\mydir\myprog.exe %1")
/// FA.Create
///
///
C# code
///
/// FileAssociation FA = new FileAssociation();
/// FA.Extension = "xyz";
/// FA.ContentType = "application/myprogram";
/// FA.FullName = "My XYZ Files!";
/// FA.ProperName = "XYZ File";
/// FA.AddCommand("open", "C:\\mydir\\myprog.exe %1");
/// FA.Create();
///
///
public class FileAssociation
{
///
public FileAssociation()
{
FileInfo = new FileType();
FileInfo.Commands.Captions = new ArrayList();
FileInfo.Commands.Commands = new ArrayList();
}
///
///
public string ProperName
{
get
{
return FileInfo.ProperName;
}
set
{
FileInfo.ProperName = value;
}
}
///
///
public string FullName
{
get
{
return FileInfo.FullName;
}
set
{
FileInfo.FullName = value;
}
}
///
///
public string ContentType
{
get
{
return FileInfo.ContentType;
}
set
{
FileInfo.ContentType = value;
}
}
///
///
///
public string Extension
{
get
{
return FileInfo.Extension;
}
set
{
if (value.Substring(0, 1) != ".")
value = "." + value;
FileInfo.Extension = value;
}
}
///
///
public short IconIndex
{
get
{
return FileInfo.IconIndex;
}
set
{
FileInfo.IconIndex = value;
}
}
///
///
///
public string IconPath
{
get
{
return FileInfo.IconPath;
}
set
{
FileInfo.IconPath = value;
}
}
///
/// The name of the command.
/// The command to execute.
///
public void AddCommand(string Caption, string Command)
{
if (Caption == null
Command == null)
throw new ArgumentNullException();
FileInfo.Commands.Captions.Add(Caption);
FileInfo.Commands.Commands.Add(Command);
}
///
///
///
///
public void Create()
{
// remove the extension to avoid incompatibilities [such as DDE links]
try
{
Remove();
}
catch (ArgumentException) { } // the extension doesn't exist
// create the exception
if (Extension == ""
ProperName == "")
throw new ArgumentException();
int cnt;
try
{
RegistryKey RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(Extension);
RegKey.SetValue("", ProperName);
if (ContentType != null && ContentType != "")
RegKey.SetValue("Content Type", ContentType);
RegKey.Close();
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName);
RegKey.SetValue("", FullName);
RegKey.Close();
if (IconPath != "")
{
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "DefaultIcon");
RegKey.SetValue("", IconPath + "," + IconIndex.ToString());
RegKey.Close();
}
for (cnt = 0; cnt < FileInfo.Commands.Captions.Count; cnt++)
{
RegKey = Microsoft.Win32.Registry.ClassesRoot.CreateSubKey(ProperName + "\\" + "Shell" + "\\" + (string)FileInfo.Commands.Captions[cnt]);
RegKey = RegKey.CreateSubKey("Command");
RegKey.SetValue("", FileInfo.Commands.Commands[cnt]);
RegKey.Close();
}
}
catch
{
throw new Exception();
}
}
///
///
///
///
public void Remove()
{
if (Extension == null
ProperName == null)
throw new ArgumentNullException();
if (Extension == ""
ProperName == "")
throw new ArgumentException();
Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(Extension);
Microsoft.Win32.Registry.ClassesRoot.DeleteSubKeyTree(ProperName);
}
///
private FileType FileInfo;
}
}
No comments:
Post a Comment