/// <summary> /// Gets or value that determines the <see cref="PerceivedType"/>PerceivedType of the file. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <returns><see cref="PerceivedTypes"/> that specifies Perceived Type of extension.</returns> protected PerceivedTypes GetPerceivedType(FileAssociationInfo file) { if (!file.Exists) { throw new Exception("Extension does not exist"); } object val = registryWrapper.Read(file.extension, "PerceivedType"); PerceivedTypes actualType = PerceivedTypes.None; if (val == null) { return(actualType); } try { actualType = (PerceivedTypes)Enum.Parse(typeof(PerceivedTypes), val.ToString(), true); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(actualType); }
/// <summary> /// Creates actual extension association key in registry for the specified extension and supplied attributes. /// </summary> /// <param name="progId">Name of expected handling program.</param> /// <param name="perceivedType"><see cref="PerceivedTypes"/>PerceivedType of file type.</param> /// <param name="contentType">MIME type of file type.</param> /// <param name="openwithList"></param> /// <returns>FileAssociationInfo instance referring to specified extension.</returns> public FileAssociationInfo Create(string progId, PerceivedTypes perceivedType, string contentType, string[] openwithList) { FileAssociationInfo fai = new FileAssociationInfo(extension); if (fai.Exists) { fai.Delete(); } fai.Create(); fai.ProgID = progId; if (perceivedType != PerceivedTypes.None) { fai.PerceivedType = perceivedType; } if (contentType != string.Empty) { fai.ContentType = contentType; } if (openwithList != null) { fai.OpenWithList = openwithList; } return(fai); }
/// <summary> /// Sets array of containing program file names which should be displayed in the Open With List. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <param name="programList">Program file names</param> protected void SetOpenWithList(FileAssociationInfo file, string[] programList) { if (!file.Exists) { throw new Exception("Extension does not exist"); } RegistryKey root = Registry.ClassesRoot; RegistryKey key = root.OpenSubKey(file.extension, true); RegistryKey tmpkey = key.OpenSubKey("OpenWithList", true); if (tmpkey != null) { key.DeleteSubKeyTree("OpenWithList"); } key = key.CreateSubKey("OpenWithList"); foreach (string s in programList) { key.CreateSubKey(s); } ShellNotification.NotifyOfChange(); }
/// <summary> /// Creates actual file extension entry in registry. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> instance that contains specifics on extension to be created.</param> protected void Create(FileAssociationInfo file) { if (file.Exists) { file.Delete(); } RegistryKey root = Registry.ClassesRoot; root.CreateSubKey(file.extension); }
/// <summary> /// Set a value that indicates the name of the associated application with the behavior to handle this extension. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <param name="progId">Associated Program ID of handling program.</param> protected void SetProgID(FileAssociationInfo file, string progId) { if (!file.Exists) { throw new Exception("Extension does not exist"); } registryWrapper.Write(file.extension, string.Empty, progId); ShellNotification.NotifyOfChange(); }
/// <summary> /// Sets a value that determines the MIME type of the file. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <param name="type">MIME content type of extension.</param> protected void SetContentType(FileAssociationInfo file, string type) { if (!file.Exists) { throw new Exception("Extension does not exist"); } registryWrapper.Write(file.extension, "Content Type", type); ShellNotification.NotifyOfChange(); }
/// <summary> /// Sets a value that determines the <see cref="PerceivedType"/>PerceivedType of the file. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <param name="type"><see cref="PerceivedTypes"/> to be set that specifies Perceived Type of extension.</param> protected void SetPerceivedType(FileAssociationInfo file, PerceivedTypes type) { if (!file.Exists) { throw new Exception("Extension does not exist"); } registryWrapper.Write(file.extension, "PerceivedType", type.ToString()); ShellNotification.NotifyOfChange(); }
/// <summary> /// Associates an alprêt existing program id with a list of extensions. /// </summary> /// <param name="progId">The program id to associate extensions with.</param> /// <param name="extensions">String array of extensions to associate with program id.</param> public void Associate(string progId, params string[] extensions) { foreach (string s in extensions) { FileAssociationInfo fai = new FileAssociationInfo(s); if (!fai.Exists) { fai.Create(progId); } fai.ProgID = progId; } }
/// <summary> /// Deletes actual file extension entry in registry. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> instance that contains specifics on extension to be deleted.</param> protected void Delete(FileAssociationInfo file) { try { if (!file.Exists) { throw new Exception("Key not found."); } RegistryKey root = Registry.ClassesRoot; root.DeleteSubKeyTree(file.extension); } catch { } }
/// <summary> /// Gets a value that indicates the name of the associated application with the behavior to handle this extension. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <returns>Associated Program ID of handling program.</returns> protected string GetProgID(FileAssociationInfo file) { if (!file.Exists) { throw new Exception("Extension does not exist"); } object val = registryWrapper.Read(file.extension, string.Empty); if (val == null) { return(string.Empty); } return(val.ToString()); }
/// <summary> /// Sets a value that indicates the filter component that is used to search for text within documents of this type. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <param name="persistentHandler">Guid of filter component.</param> protected void SetPersistentHandler(FileAssociationInfo file, Guid persistentHandler) { if (!file.Exists) { throw new Exception("Extension does not exist"); } if (persistentHandler == Guid.Empty) { return; } this.registryWrapper.Write(file.extension + "\\" + PersistentHandler, string.Empty, persistentHandler); ShellNotification.NotifyOfChange(); }
/// <summary> /// Verifies that given extension exists and is associated with given program id /// </summary> /// <param name="extension">Extension to be checked for.</param> /// <param name="progId">progId to be checked for.</param> /// <returns>True if association exists, false if it does not.</returns> public bool IsValid(string extension, string progId) { FileAssociationInfo fai = new FileAssociationInfo(extension); if (!fai.Exists) { return(false); } if (progId != fai.ProgID) { return(false); } return(true); }
/// <summary> /// Determines of the list of extensions are associated with the specified program id. /// </summary> /// <param name="progId">Program id to check against.</param> /// <param name="extensions">String array of extensions to check against the program id.</param> /// <returns>String array of extensions that were not associated with the program id.</returns> public string[] CheckAssociation(string progId, params string[] extensions) { List <string> notAssociated = new List <string>(); foreach (string s in extensions) { FileAssociationInfo fai = new FileAssociationInfo(s); if (!fai.Exists || fai.ProgID != progId) { notAssociated.Add(s); } } return(notAssociated.ToArray()); }
/// <summary> /// Gets a value that indicates the filter component that is used to search for text within documents of this type. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <returns>Guid of filter component.</returns> protected Guid GetPersistentHandler(FileAssociationInfo file) { if (!file.Exists) { throw new Exception("Extension does not exist"); } object val = registryWrapper.Read(file.extension + "\\PersistentHandler", string.Empty); if (val == null) { return(new Guid()); } else { return(new Guid(val.ToString())); } }
/// <summary> /// Gets array of containing program file names which should be displayed in the Open With List. /// </summary> /// <param name="file"><see cref="FileAssociationInfo"/> that provides specifics of the extension to be changed.</param> /// <returns>Program file names</returns> protected string[] GetOpenWithList(FileAssociationInfo file) { if (!file.Exists) { throw new Exception("Extension does not exist"); } RegistryKey root = Registry.ClassesRoot; RegistryKey key = root.OpenSubKey(file.extension); key = key.OpenSubKey("OpenWithList"); if (key == null) { return(new string[0]); } return(key.GetSubKeyNames()); }
/// <summary> /// Associates a single executable with a list of extensions. /// </summary> /// <param name="progId">Name of program id</param> /// <param name="executablePath">Path to executable to start including arguments.</param> /// <param name="extensions">String array of extensions to associate with program id.</param> /// <example>progId = "MyTextFile" /// executablePath = "notepad.exe %1" /// extensions = ".txt", ".text"</example> public void Associate(string progId, string executablePath, params string[] extensions) { foreach (string s in extensions) { FileAssociationInfo fai = new FileAssociationInfo(s); if (!fai.Exists) { fai.Create(progId); } fai.ProgID = progId; } ProgramAssociationInfo pai = new ProgramAssociationInfo(progId); if (!pai.Exists) { pai.Create(); } pai.AddVerb(new ProgramVerb("open", executablePath)); }