상세 컨텐츠

본문 제목

확장자에 대한 기본프로그램 등록

C#

by 탑~! 2018. 6. 29. 17:35

본문

using System.IO;
using Microsoft.Win32;
ProcessFileExtReg(true);
private static void ProcessFileExtReg(bool register)
        {
            using (RegistryKey classesKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes"true))
            {
                if (CheckRegistryKeyExists(classesKey, ext)) return;
 
                if (register == true)
                {
                    using (RegistryKey extKey = classesKey.CreateSubKey(ext))
                    {
                        extKey.SetValue(null, extType);
                    }
 
                    // or, use Registry.SetValue method
                    using (RegistryKey typeKey = classesKey.CreateSubKey(extType))
                    {
                        typeKey.SetValue(null, fileTypeDesc);
                        using (RegistryKey shellKey = typeKey.CreateSubKey("shell"))
                        {
                            using (RegistryKey openKey = shellKey.CreateSubKey("open"))
                            {
                                using (RegistryKey commandKey = openKey.CreateSubKey("command"))
                                {
                                    string assocExePath = GetProcessPath();
                                    string assocCommand = string.Format("\"{0}\" \"%1\"", assocExePath);
 
                                    commandKey.SetValue(null, assocCommand);
                                }
                            }
                        }
                    }
                }
                else
                {
                    DeleteRegistryKey(classesKey, ext, false);
                    DeleteRegistryKey(classesKey, extType, true);
                }
            }
        }
 
        private static void DeleteRegistryKey(RegistryKey classesKey, string subKeyName, bool deleteAllSubKey)
        {
            if (CheckRegistryKeyExists(classesKey, subKeyName) == false)
            {
                return;
            }
 
            if (deleteAllSubKey == true)
            {
                classesKey.DeleteSubKeyTree(subKeyName);
            }
            else
            {
                classesKey.DeleteSubKey(subKeyName);
            }
        }
 
        private static bool CheckRegistryKeyExists(RegistryKey classesKey, string subKeyName)
        {
            RegistryKey regKey = null;
 
            try
            {
                regKey = classesKey.OpenSubKey(subKeyName);
                return regKey != null;
            }
            finally
            {
                if (regKey != null)
                {
                    regKey.Close();
                }
            }
        }
 
        private static string GetProcessPath()
        {
            string path = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            return Path.Combine(path, assocExeFileName);
        }


'C#' 카테고리의 다른 글

C# 동적메모리 할당  (0) 2018.07.04
[.Net 4.5] 2Gb 이상 메모리 사용 하기  (0) 2018.07.03
웹 크롤링  (0) 2018.06.29
yield  (0) 2018.06.29
FileStream 비동기 입출력  (0) 2018.06.29

관련글 더보기