Mk0.Software.ImageSorter/Mk0.Software.ImageSorter/FileAssociation.cs

65 lines
2.4 KiB
C#
Raw Normal View History

2019-04-05 10:52:47 +02:00
using Microsoft.Win32;
using System;
namespace Mk0.Tools.FileAssociaton
{
class FileAssociation
{
[System.Runtime.InteropServices.DllImport("Shell32.dll")]
private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
private const int SHCNE_ASSOCCHANGED = 0x8000000;
private const int SHCNF_FLUSH = 0x1000;
public static void Add(string progId, string extension, string applicationFilePath, string fileTypeDescription, string iconPath)
{
bool madeChanges = false;
madeChanges |= SetAssociation(
extension,
progId,
fileTypeDescription,
2019-04-05 21:16:57 +02:00
applicationFilePath,
iconPath);
2019-04-05 10:52:47 +02:00
if (madeChanges)
{
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_FLUSH, IntPtr.Zero, IntPtr.Zero);
}
}
public static void Remove(string progId, string extension, string applicationFilePath, string fileTypeDescription, string iconPath)
{
2019-04-05 21:18:55 +02:00
//remove reg todo
2019-04-05 10:52:47 +02:00
}
public static void Check(string progId, string extension, string applicationFilePath, string fileTypeDescription, string iconPath)
{
2019-04-05 21:18:55 +02:00
//add or update reg todo
2019-04-05 10:52:47 +02:00
}
2019-04-05 21:16:57 +02:00
private static bool SetAssociation(string extension, string progId, string fileTypeDescription, string applicationFilePath, string iconPath)
2019-04-05 10:52:47 +02:00
{
bool madeChanges = false;
madeChanges |= SetKeyDefaultValue(@"Software\Classes\" + extension, progId);
madeChanges |= SetKeyDefaultValue(@"Software\Classes\" + progId, fileTypeDescription);
madeChanges |= SetKeyDefaultValue($@"Software\Classes\{progId}\shell\open\command", "\"" + applicationFilePath + "\" \"%1\"");
2019-04-05 21:16:57 +02:00
madeChanges |= SetKeyDefaultValue($@"Software\Classes\{progId}\DefaultIcon", "\"" + iconPath + "\"");
2019-04-05 10:52:47 +02:00
return madeChanges;
}
private static bool SetKeyDefaultValue(string keyPath, string value)
{
using (var key = Registry.CurrentUser.CreateSubKey(keyPath))
{
if (key.GetValue(null) as string != value)
{
key.SetValue(null, value);
return true;
}
}
return false;
}
}
}