Delete (depending on settings set) and check (every startup) file assoc in Registry

This commit is contained in:
Manuel Kamper 2019-12-27 14:59:29 +01:00
parent 96eedf6c39
commit b68a4da9b7
2 changed files with 82 additions and 12 deletions

View File

@ -27,14 +27,22 @@ namespace Mk0.Tools.FileAssociaton
}
}
public static void Remove(string progId, string extension, string applicationFilePath, string fileTypeDescription, string iconPath)
public static void Remove(string progId)
{
//remove reg todo
bool madeChanges = false;
madeChanges |= RemoveAssociation(progId);
if (madeChanges)
{
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_FLUSH, IntPtr.Zero, IntPtr.Zero);
}
}
public static void Check(string progId, string extension, string applicationFilePath, string fileTypeDescription, string iconPath)
public static bool Check(string progId, string applicationFilePath, string fileTypeDescription, string iconPath)
{
//add or update reg todo
bool exists = false;
exists |= CheckAssociation(progId, applicationFilePath, fileTypeDescription, iconPath);
return exists;
}
private static bool SetAssociation(string extension, string progId, string fileTypeDescription, string applicationFilePath, string iconPath)
@ -47,6 +55,20 @@ namespace Mk0.Tools.FileAssociaton
return madeChanges;
}
private static bool RemoveAssociation(string progId)
{
bool madeChanges = false;
madeChanges |= DeleteKeyDefaultValue($@"Software\Classes\", progId);
return madeChanges;
}
private static bool CheckAssociation(string progId, string applicationFilePath, string fileTypeDescription, string iconPath)
{
bool exists = false;
exists |= CheckKeyDefaultValue($@"Software\Classes\{progId}\shell\open\command", "\"" + applicationFilePath + "\" \"%1\"");
return exists;
}
private static bool SetKeyDefaultValue(string keyPath, string value)
{
using (var key = Registry.CurrentUser.CreateSubKey(keyPath))
@ -60,5 +82,38 @@ namespace Mk0.Tools.FileAssociaton
return false;
}
private static bool DeleteKeyDefaultValue(string keyPath, string value)
{
bool erfolg = false;
using (var key = Registry.CurrentUser.OpenSubKey(keyPath, writable: true))
{
if (key != null)
{
key.DeleteSubKeyTree(value, false);
erfolg = true;
}
}
return erfolg;
}
private static bool CheckKeyDefaultValue(string keyPath, string value)
{
bool exists = false;
using (var key = Registry.CurrentUser.OpenSubKey(keyPath, writable: true))
{
if (key != null)
{
string x = (string)key.GetValue("");
if (!string.IsNullOrEmpty(x) && x == value)
{
exists = true;
}
}
}
return exists;
}
}
}

View File

@ -53,7 +53,22 @@ namespace Mk0.Software.ImageSorter
SetDefaultPath();
comboBoxZoom.SelectedIndex = Properties.Settings.Default.zoom;
//todo file assoc prüfen und in einstellungen richtig setzen
//file assoc prüfen und in einstellungen richtig setzen
if(FileAssociation.Check("Image_Sorter_JPG", Application.ExecutablePath, "JPG Bild", $@"{Application.StartupPath}\AssocIcons\jpg.ico") &&
FileAssociation.Check("Image_Sorter_PNG", Application.ExecutablePath, "PNG Bild", $@"{Application.StartupPath}\AssocIcons\png.ico") &&
FileAssociation.Check("Image_Sorter_GIF", Application.ExecutablePath, "GIF Bild", $@"{Application.StartupPath}\AssocIcons\gif.ico") &&
FileAssociation.Check("Image_Sorter_JPEG", Application.ExecutablePath, "JPEG Bild", $@"{Application.StartupPath}\AssocIcons\jpeg.ico") &&
FileAssociation.Check("Image_Sorter_BMP", Application.ExecutablePath, "BMP Bild", $@"{Application.StartupPath}\AssocIcons\bmp.ico") &&
FileAssociation.Check("Image_Sorter_TIF", Application.ExecutablePath, "TIF Bild", $@"{Application.StartupPath}\AssocIcons\tif.ico") &&
FileAssociation.Check("Image_Sorter_TIFF", Application.ExecutablePath, "TIFF Bild", $@"{Application.StartupPath}\AssocIcons\tiff.ico"))
{
Properties.Settings.Default.fileAssociation = true;
}
else
{
Properties.Settings.Default.fileAssociation = false;
}
Properties.Settings.Default.Save();
}
private void Main_Load(object sender, EventArgs e)
@ -399,13 +414,13 @@ namespace Mk0.Software.ImageSorter
if(Directory.Exists($@"{Application.StartupPath}\AssocIcons"))
{
Directory.Delete($@"{Application.StartupPath}\AssocIcons", true);
FileAssociation.Remove("Image_Sorter_JPG", ".jpg", Application.ExecutablePath, "JPG Bild", $@"{Application.StartupPath}\AssocIcons\jpg.ico");
FileAssociation.Remove("Image_Sorter_PNG", ".png", Application.ExecutablePath, "PNG Bild", $@"{Application.StartupPath}\AssocIcons\png.ico");
FileAssociation.Remove("Image_Sorter_GIF", ".gif", Application.ExecutablePath, "GIF Bild", $@"{Application.StartupPath}\AssocIcons\gif.ico");
FileAssociation.Remove("Image_Sorter_JPEG", ".jpeg", Application.ExecutablePath, "JPEG Bild", $@"{Application.StartupPath}\AssocIcons\jpeg.ico");
FileAssociation.Remove("Image_Sorter_BMP", ".bmp", Application.ExecutablePath, "BMP Bild", $@"{Application.StartupPath}\AssocIcons\bmp.ico");
FileAssociation.Remove("Image_Sorter_TIF", ".tif", Application.ExecutablePath, "TIF Bild", $@"{Application.StartupPath}\AssocIcons\tif.ico");
FileAssociation.Remove("Image_Sorter_TIFF", ".tiff", Application.ExecutablePath, "TIFF Bild", $@"{Application.StartupPath}\AssocIcons\tiff.ico");
FileAssociation.Remove("Image_Sorter_JPG");
FileAssociation.Remove("Image_Sorter_PNG");
FileAssociation.Remove("Image_Sorter_GIF");
FileAssociation.Remove("Image_Sorter_JPEG");
FileAssociation.Remove("Image_Sorter_BMP");
FileAssociation.Remove("Image_Sorter_TIF");
FileAssociation.Remove("Image_Sorter_TIFF");
}
}