Wednesday, September 22, 2010

File Association

http://www.codeproject.com/KB/files/custom-file-extension.aspx

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;

}



/// Properties of the file association.

internal struct FileType

{

public CommandArrayList Commands;

public string Extension;

public string ProperName;

public string FullName;

public string ContentType;

public string IconPath;

public short IconIndex;



}



/// Creates file associations for your programs.



/// The following example creates a file association for the type XYZ with a non-existent program.



///


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

{



/// Initializes an instance of the FileAssociation class.



public FileAssociation()

{



FileInfo = new FileType();



FileInfo.Commands.Captions = new ArrayList();



FileInfo.Commands.Commands = new ArrayList();



}



/// Gets or sets the proper name of the file type.



/// A String representing the proper name of the file type.



public string ProperName

{



get

{



return FileInfo.ProperName;



}



set

{



FileInfo.ProperName = value;



}



}



/// Gets or sets the full name of the file type.



/// A String representing the full name of the file type.



public string FullName

{



get

{



return FileInfo.FullName;



}



set

{



FileInfo.FullName = value;



}



}



/// Gets or sets the content type of the file type.



/// A String representing the content type of the file type.



public string ContentType

{



get

{



return FileInfo.ContentType;



}



set

{



FileInfo.ContentType = value;



}



}



/// Gets or sets the extension of the file type.



/// A String representing the extension of the file type.



/// If the extension doesn't start with a dot ("."), a dot is automatically added.



public string Extension

{



get

{



return FileInfo.Extension;



}



set

{



if (value.Substring(0, 1) != ".")



value = "." + value;



FileInfo.Extension = value;



}



}



/// Gets or sets the index of the icon of the file type.



/// A short representing the index of the icon of the file type.



public short IconIndex

{



get

{



return FileInfo.IconIndex;



}



set

{



FileInfo.IconIndex = value;



}



}



/// Gets or sets the path of the resource that contains the icon for the file type.



/// A String representing the path of the resource that contains the icon for the file type.



/// This resource can be an executable or a DLL.



public string IconPath

{



get

{



return FileInfo.IconPath;



}



set

{



FileInfo.IconPath = value;



}



}



/// Adds a new command to the command list.



/// The name of the command.



/// The command to execute.



/// Caption -or- Command is null (VB.NET: Nothing).



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);



}



/// Creates the file association.



/// Extension -or- ProperName is null (VB.NET: Nothing).



/// Extension -or- ProperName is empty.



/// The user does not have registry write access.



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();



}



}



/// Removes the file association.



/// Extension -or- ProperName is null (VB.NET: Nothing).



/// Extension -or- ProperName is empty -or- the specified extension doesn't exist.



/// The user does not have registry delete access.



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);



}



/// Holds the properties of the file type.



private FileType FileInfo;



}



}

No comments:

Post a Comment