본문 바로가기
개발언어/C#.NET

프로그램 시작에 등록하기

by 엔돌슨 2011. 10. 21.
반응형

시작메뉴에 프로그램 등록하기


클릭원스로 배포를 하고 프로그램을 윈도우 시작시마다 실행하게 하고 싶었다. 그런데 레지스터에 기록하고 빼게 작성을 해두었더니 윈도우 계정중 administrator 계정이 아니거나 레지스트 권한이 없을 경우 처리할 수 없었다. 그래서 생각한것이 프로그램의 단축 아이콘을 만들고 그 아이콘을 시작 > 시작프로그램 폴더에 복사하여 넣는 것이다. 꼼수란 편하다. 하지만 예기치 못한 결과가 있을 수 있으니 테스트가 중요하다.




시작프로그램에 MyPeople 처럼 단축아이콘을 넣는 것이다. 제작한 프로그램의 환경설정에서 시작할때 실행하기 체크를 하면 복사하여 준다. 단, 클릭원스의 URL을 사용하니 프로그램의 단축아이콘을 생성하는 부분을 수정하면 된다.


간단하게 만들어본 데모이다.

코드는 아래와 같다.

private void CreateWindowsStartup(bool create)
        {
            string startupFileName = Environment.GetFolderPath(System.Environment.SpecialFolder.Startup) + "\\" + Application.ProductName + ".appref-ms";
            //string applicationShortcut = ApplicationDeployment.CurrentDeployment.ActivationUri.ToString();
            string applicationShortcut = "http://아이피:8080/Messenger/Messenger.application#Messenger.application, Culture=neutral, PublicKeyToken=0000000000000000, processorArchitecture=msil";
       
            if (create)
            {
                if (!File.Exists(startupFileName))
                {
                    using (FileStream fs = new FileStream(startupFileName, FileMode.Create))
                    {
                        using (StreamWriter sw = new StreamWriter(fs, Encoding.Unicode))
                        {
                            sw.WriteLine(applicationShortcut);
                        }
                    }
                }
            }
            else
            {
                if (File.Exists(startupFileName))
                {
                    File.Delete(startupFileName);
                }
            }
        }


레지스트를 이용할 수 있다면
static void CheckForShortcut()
        {
            if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
            {
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
                if (ad.IsFirstRun)  //first time user has run the app since installation or update
                {
                    Assembly code = Assembly.GetExecutingAssembly();
                    string company = string.Empty;
                    string description = string.Empty;
                    if (Attribute.IsDefined(code, typeof(AssemblyCompanyAttribute)))
                    {
                        AssemblyCompanyAttribute ascompany =
                            (AssemblyCompanyAttribute)Attribute.GetCustomAttribute(code,
                            typeof(AssemblyCompanyAttribute));
                        company = ascompany.Company;
                    }
                    if (Attribute.IsDefined(code, typeof(AssemblyDescriptionAttribute)))
                    {
                        AssemblyDescriptionAttribute asdescription =
                            (AssemblyDescriptionAttribute)Attribute.GetCustomAttribute(code,
                            typeof(AssemblyDescriptionAttribute));
                        description = asdescription.Description;
                    }
                    if (company != string.Empty && description != string.Empty)
                    {
                        string desktopPath = string.Empty;
                        desktopPath = string.Concat(
                            Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                            "\\", description, ".appref-ms");
                        string shortcutName = string.Empty;
                        shortcutName = string.Concat(
                            Environment.GetFolderPath(Environment.SpecialFolder.Programs),
                            "\\", company, "\\", description, ".appref-ms");
                        System.IO.File.Copy(shortcutName, desktopPath, true);
                    }
                }
            }
        }

        private void manage_startupAutoRun(string action)
        {
            string startMenu_FolderName = "회사명(주)";
            if (string.IsNullOrEmpty(startMenu_FolderName))
                startMenu_FolderName = "메신저"; //NOTE: startMenu_FolderName must be set to the value of Project Properties > Publish Tab > Options button > Publisher Name field
            //string app_program_file = Environment.GetFolderPath(System.Environment.SpecialFolder.Programs) + "\\" + startMenu_FolderName + "\\" + Application.ProductName + ".appref-ms";
            string app_program_file = Environment.GetFolderPath(System.Environment.SpecialFolder.Programs) + "\\" + startMenu_FolderName + "\\" + "메신저\\메신저.appref-ms";
            bool app_program_file_exists = System.IO.File.Exists(app_program_file);
            string startup_file = Environment.GetFolderPath(System.Environment.SpecialFolder.Startup) + "\\" + Application.ProductName + ".appref-ms";
            bool startup_file_exists = File.Exists(startup_file);
            startMenu_FolderName = string.Empty;
            switch (action)
            {
                case "delete":
                    //remove application reference shortcut
                    if (startup_file_exists) File.Delete(startup_file);
                    break;
                case "copy":
                    if ((app_program_file_exists) && (!startup_file_exists))
                        File.Copy(app_program_file, startup_file);
                    //copy application reference shortcut
                    break;
                default:
                    throw new Exception("An invalid paramater [" + action + "] was passed to the manage_startupAutoRun method: accepts \"copy\" or \"delete\"");
                    break;
            }
        }

이렇게 작성이 가능하다.