상세 컨텐츠

본문 제목

기본 앱 설정

C#

by 탑~! 2018. 6. 23. 14:40

본문

using System.IO;

using Microsoft.Win32;


namespace YourExtRegister

{

    class Program

    {

        static string ext = ".1myext";

        static string fileTypeDesc = "my ext sample";

        static string extType = "yourext" + ext + ".v1";

        static string assocExeFileName = "c:\\YourExt.exe";


        static void Main(string[] args)

        {

            bool register = true;


            if (args.Length >= 1)

            {

                if (args[0] == "-u")

                {

                    register = false;

                }

            }


            ProcessFileExtReg(register);

        }


        private static void ProcessFileExtReg(bool register)

        {

            using (RegistryKey classesKey = Registry.CurrentUser.OpenSubKey(@"Software\Classes", true))

            {

                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#' 카테고리의 다른 글

MemoryStream Compress  (0) 2018.06.29
C# DataSet 압축  (0) 2018.06.29
Keyboard Hook  (0) 2018.06.07
[C#] 세가지 Timer 와 그 차이점  (0) 2018.06.05
VS2008 Project Error  (0) 2017.11.01

관련글 더보기