Доброе время суток. Сегодня встала задача добавить в список "Открыть с помощью"/"OpenWithList" для определённому расширению программу. Немного помучившись, получилась следующая функция. # Добавления в ручную:| Цитата | Во-первых: Переходим по следующему пути [HKEY_CLASSES_ROOT\Applications] - Создаём следующий путь: [HKEY_CLASSES_ROOT\Applications\ProgramName.exe\shell\open\command] - default vaule: "FilePath" "%1" Во-вторых: Переходим по следующему пути [HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.extension\OpenWithList] - Создаём строковое поле, имя которого будет латинская буква алфавита. - Записываем в это поле имя нашей программы, которое мы указали при создание в пункте №1. - Затем добавляем в строковое поле "MRUList" нашу букву.
|
# Задача: Добавить для определённого расширения в функцию "Открыть с помощью" в раздел "Рекомендуемые программы" программу. Чтобы можно было быстро открыть файл через программу. | Код | private void Form_Load(object sender, EventArgs e) { // Вызов функции; AddProgramToOpenWithList("txt", Application.ExecutablePath); } ..... .....
public static bool AddProgramToOpenWithList(string sExtension, string FilePath) { bool Result = true; RegistryKey pReg = null; // ---- string FilePathReg = "\"" + FilePath + "\" \"%1\""; string FilePathCompare = FilePath.ToLower(); byte[] bchar = Encoding.ASCII.GetBytes("a"); // ---- string sExt = "." + sExtension.ToLower(); string sAppPath = "Applications\\"; string sExtPath = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + sExt + "\\OpenWithList"; string sOPath = "\\shell\\open\\command\\"; string sOWLName = "MRUList"; // ---- int i = 0, maxlen = 0; string sTemp = string.Empty; string sTempName = string.Empty; string sProgramName = string.Empty; FileInfo fInfo = null; string[] sSubKeys = null; bool bResult = false; // ---- try { fInfo = new FileInfo(FilePath); // ---- sProgramName = fInfo.Name; // ---- // Check Applications Name settings in HKCR; // ---- sTemp = sAppPath + sProgramName + sOPath; pReg = Registry.ClassesRoot.OpenSubKey(sTemp, true); if (pReg == null) pReg = Registry.ClassesRoot.CreateSubKey(sTemp); // ---- if (pReg.GetValue(null) != null) { sTemp = pReg.GetValue(null).ToString(); sTemp = sTemp.ToLower(); // ---- if (sTemp.IndexOf(FilePathCompare) < 0) pReg.SetValue(null, FilePathReg); } else pReg.SetValue(null, FilePathReg); // ---- pReg.Close(); // ---- // Check FileName in OpenWithList; // ---- pReg = Registry.CurrentUser.OpenSubKey(sExtPath, true); if (pReg != null) { sSubKeys = pReg.GetValueNames(); // ---- for (i = 0; i < sSubKeys.Length; i++) { sTemp = pReg.GetValue(sSubKeys[i]).ToString(); // ---- if (sTemp.ToLower() == sProgramName.ToLower()) { if (pReg.GetValue(sOWLName) != null) { sTemp = pReg.GetValue(sOWLName).ToString(); // ---- if (sTemp.IndexOf(sSubKeys[i]) < 0) { sTemp += sSubKeys[i]; pReg.SetValue(sOWLName, sTemp); } } else pReg.SetValue(sOWLName, sSubKeys[i]); // ---- bResult = true; // ---- break; } } // ---- if (!bResult) { i = bchar[0]; maxlen = i + 26; sTempName = string.Empty; // ---- for (; i < maxlen; i++) { sTemp = Convert.ToChar(i).ToString(); // ---- if (pReg.GetValue(sTemp) == null) { sTempName = sTemp; pReg.SetValue(sTempName, sProgramName); break; } } // ---- if (pReg.GetValue(sOWLName) != null) { sTemp = pReg.GetValue(sOWLName).ToString(); // ---- if (sTemp.IndexOf(sTempName) < 0) { sTemp += sTempName; pReg.SetValue(sOWLName, sTemp); } } else pReg.SetValue(sOWLName, sTempName); } } // ---- pReg.Close(); } catch (Exception) { Result = false; } // ---- if (pReg != null) pReg.Close(); // ---- return Result; } // ----------------------------------------------------------------------
|
# Тестирование:| Цитата | Данная функция тестировалась на следующих операционных системах:
- Windows XP Professional SP3;
- Windows Vista Home SP1;
|
P.S. -> Рад конструктивной критики, советам и т.д. Это сообщение отредактировал(а) MuForum - 1.8.2009, 16:30
--------------------
"Чтобы правильно задать вопрос, нужно знать большую часть ответа!" (Р. Шекли)
|