@GameDev_Easy
Сегодня я пишу на змеях...

Как программно ассоциировать расширение с приложением на C#?

Как можно программно ассоциировать расширение (например, ".abc") с приложением?
  • Вопрос задан
  • 278 просмотров
Решения вопроса 2
mindtester
@mindtester Куратор тега C#
http://iczin.su/hexagram_48
1 - решение все таки гуглится (надо обобщить, убрать C#, для начала)
2 - www.cyberforum.ru/visual-basic/thread1088605.html разберите предложенный алгоритм, и, думаю, все получится
Ответ написан
mshak
@mshak
я делал так:
internal static class UnsafeNativeMethods
{
    [DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    internal static extern void SHChangeNotify(uint wEventId, uint uFlags, IntPtr dwItem1, IntPtr dwItem2);
}

public class FileExtensionRegistrator
{
	public const string fileExtension = ".bla";
	public const string keyName = "bla_bla_file";
	public const string fileDescription = "It is my BLA file";

	public static void SetAssociation(string openWith)
	{
		SetAssociation(fileExtension, keyName, openWith, fileDescription);
	}

	public static void SetAssociation(string extension, string keyName, string openWith, string fileDescription)
	{
		RegistryKey baseKey;
		RegistryKey openMethod;
		RegistryKey shell;
		RegistryKey currentUser;

		baseKey = Registry.ClassesRoot.CreateSubKey(extension);
		baseKey.SetValue("", keyName);

		openMethod = Registry.ClassesRoot.CreateSubKey(keyName);
		openMethod.SetValue("", fileDescription);
		openMethod.CreateSubKey("DefaultIcon").SetValue("", "\"" + openWith + "\",0");
		shell = openMethod.CreateSubKey("Shell");
		shell.CreateSubKey("open").CreateSubKey("command").SetValue("", string.Format("\"{0}\" -cmd=open -file=\"%1\"",openWith));
		baseKey.Close();
		openMethod.Close();
		shell.Close();

		// Delete the key instead of trying to change it
		currentUser = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension, true);
		if (currentUser != null)
		{
			currentUser.DeleteSubKey("UserChoice", false);
			currentUser.Close();
		}
		// Tell explorer the file association has been changed
		UnsafeNativeMethods.SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
	}

	public static void RemoveAssociation()
	{
		RemoveAssociation(fileExtension, keyName);
	}

	public static void RemoveAssociation(string extension, string keyName)
	{
		try
		{
			Registry.ClassesRoot.DeleteSubKeyTree(extension);
			Registry.ClassesRoot.DeleteSubKeyTree(keyName);
		}
		catch {}

		// Tell explorer the file association has been changed
		UnsafeNativeMethods.SHChangeNotify(0x08000000, 0x0000, IntPtr.Zero, IntPtr.Zero);
	}
}


Вызывался так:
FileExtensionRegistrator.SetAssociation(Application.ExecutablePath);


PS: Этот код писался давно, обратите внимание что в некоторым местах надо переделать на using
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы