I develop an App that creates files with a .klm extension, what I need to do is that when I click on this type of files my application opens ... How can I do it ???
I develop an App that creates files with a .klm extension, what I need to do is that when I click on this type of files my application opens ... How can I do it ???
Solve ... Invoking the Associate Method from the Load event of my form
public void Asociar()
{
if (GetProgIdFromExtension(".jll") == null && GetProgIdFromExtension(".jll") == "")
{
LinkExtension(".jll", "SudokuEdu", "SudokuEdu", "open", "SudokuEduDesc");
}
}
public void LinkExtension(string extension, string executableFileName, string programId,string command, string description = "")
{
string linkedProgramID;
RegistryKey registryKey = null;
RegistryKey registryKeyShell = null;
// El comando predeterminado es open
if (string.IsNullOrEmpty(command))
command = "open";
// Obtiene la descripción
if (string.IsNullOrEmpty(description))
description = $"{extension} Descripción de {programId}";
// Normaliza la extensión
if (!extension.StartsWith("."))
extension = "." + extension;
// Obtiene el ID del programa a partir de la extensión
linkedProgramID = GetProgIdFromExtension(extension);
// Si no hay nada asociado, se crean las claves, si hay algo asociado se modifican
if (string.IsNullOrEmpty(linkedProgramID) || linkedProgramID.Length == 0)
{
// Crear la clave con la extensión
registryKey = Registry.ClassesRoot.CreateSubKey(extension);
registryKey?.SetValue("", programId);
// Crea la clave con el programa
registryKey = Registry.ClassesRoot.CreateSubKey(programId);
registryKey?.SetValue("", description);
// Crea la clave con el comando
registryKeyShell = registryKey?.CreateSubKey($"shell\{command}\command");
}
else
{
// Abrimos la clave indicando que vamos a escribir para que nos permita crear nuevas subclaves.
registryKey = Registry.ClassesRoot.OpenSubKey(linkedProgramID, true);
registryKeyShell = registryKey?.OpenSubKey($"shell\{command}\command", true);
// Si es un comando que se añade, no existirá
if (registryKeyShell == null)
registryKeyShell = registryKey?.CreateSubKey(programId);
}
// Si tenemos la clave de registro del Shell
if (registryKeyShell != null)
{
registryKeyShell.SetValue("", $"\"{executableFileName}\" \"%1\"");
registryKeyShell.Close();
}
}
/// Método para obtener el ID de programa de una extensión
private string GetProgIdFromExtension(string extension)
{
string strProgramID = "";
// Obtiene el ID del programa
using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension))
{
if (registryKey?.GetValue("") != null)
{
// Obtiene el ID
strProgramID = registryKey.GetValue("").ToString();
// Cierra la clave
registryKey.Close();
}
}
// Devuelve el ID del programa
return strProgramID;
}
To associate an application with an extension of a file you will have to use the Widnows registry, specifically the branch shell
.
You can use this code:
using Microsoft.Win32;
/// <summary>
/// Asocia una extensión con un ejecutable y una acción
/// </summary>
public void LinkExtension(string extension, string executableFileName, string programId,
string command = "open", string description = "")
{
string linkedProgramID;
RegistryKey registryKey = null;
RegistryKey registryKeyShell = null;
// El comando predeterminado es open
if (string.IsNullOrEmpty(command))
command = "open";
// Obtiene la descripción
if (string.IsNullOrEmpty(description))
description = $"{extension} Descripción de {programId}";
// Normaliza la extensión
if (!extension.StartsWith("."))
extension = "." + extension;
// Obtiene el ID del programa a partir de la extensión
linkedProgramID = GetProgIdFromExtension(extension);
// Si no hay nada asociado, se crean las claves, si hay algo asociado se modifican
if (string.IsNullOrEmpty(linkedProgramID) || linkedProgramID.Length == 0)
{
// Crear la clave con la extensión
registryKey = Registry.ClassesRoot.CreateSubKey(extension);
registryKey?.SetValue("", programId);
// Crea la clave con el programa
registryKey = Registry.ClassesRoot.CreateSubKey(programId);
registryKey?.SetValue("", description);
// Crea la clave con el comando
registryKeyShell = registryKey?.CreateSubKey($"shell\{command}\command");
}
else
{
// Abrimos la clave indicando que vamos a escribir para que nos permita crear nuevas subclaves.
registryKey = Registry.ClassesRoot.OpenSubKey(linkedProgramID, true);
registryKeyShell = registryKey?.OpenSubKey($"shell\{command}\command", true);
// Si es un comando que se añade, no existirá
if (registryKeyShell == null)
registryKeyShell = registryKey?.CreateSubKey(programId);
}
// Si tenemos la clave de registro del Shell
if (registryKeyShell != null)
{
registryKeyShell.SetValue("", $"\"{executableFileName}\" \"%1\"");
registryKeyShell.Close();
}
}
/// <summary>
/// Método para obtener el ID de programa de una extensión
/// </summary>
private string GetProgIdFromExtension(string extension)
{
string strProgramID = "";
// Obtiene el ID del programa
using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(extension))
{
if (registryKey?.GetValue("") != null)
{
// Obtiene el ID
strProgramID = registryKey.GetValue("").ToString();
// Cierra la clave
registryKey.Close();
}
}
// Devuelve el ID del programa
return strProgramID;
}