2013-02-10 4 views
1

시작할 때 내 응용 프로그램을 실행하려고했습니다. 이 기능을 켜고 끌 수있는 컨텍스트 메뉴를 추가했습니다. 상황에 맞는 메뉴의 왼쪽에는 "확인"기능이 활성화되어 있습니다 (선택하면 체크 표시가 나타납니다).시작시 응용 프로그램 실행

// 
    // menu_startup 
    // 
    this.menu_startup.Name = "menu_startup"; 
    this.menu_startup.ShortcutKeyDisplayString = ""; 
    this.menu_startup.Size = new System.Drawing.Size(201, 22); 
    this.menu_startup.Text = "Run on startup"; 
    this.menu_startup.Click += new System.EventHandler(this.menu_startup_Click); 

는 그리고 이것은 내가 내가 잘못 여기서 뭐하는 거지 볼 수 없습니다 Form1.cs를

public string regKey = "IMGit"; 

     public Form1() 
     { 
      InitializeComponent(); 
      notifyIcon1.ContextMenuStrip = contextMenuStrip1; 

      RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 

      if (rkApp.GetValue(this.regKey) == null) 
      { 
       this.menu_startup.Checked = false; 
      } 
      else 
      { 
       this.menu_startup.Checked = true; 
      } 

      this.menu_about.Click += menu_about_Click; // Ignore 
      this.menu_exit.Click += menu_exit_Click; // Ignore 
      this.menu_startup.Click += menu_startup_Click; 
     }  

      private void menu_startup_Click(object sender, EventArgs e) 
      { 
       RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 

       if (rkApp.GetValue(this.regKey) == null) 
       { 
        rkApp.SetValue(this.regKey, Application.ExecutablePath.ToString()); 
       } 
       else 
       { 
        rkApp.DeleteValue(this.regKey, false); 
       } 
      } 

에서했던 것입니다. 이 내 응용 프로그램에 대한 새 레지스트리 항목을 설정해야합니다.

생성자에서 레지스트리 항목을 만들 때 코드 줄을 추가하면 항목이 올바르게 생성됩니다.

아이디어가 있으십니까?

+0

프로젝트 설정을 시도 했습니까? 거기에 값을주고 해당 값과 같은지 확인하고 설정을로드하십시오. – coder

+0

어떻게해야할지 모르겠다. – Aborted

+0

다음 링크는 http://msdn.microsoft.com/en-us/library/vstudio/25zf0ze8(v=vs.100).aspx – coder

답변

0

생성자에서 람다 함수를 작성하면 문제가 해결됩니다.

this.menu_startup.Click += (s, ea) => 
    { 
     RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
     string appPath = Application.ExecutablePath.ToString(); 

     this.menu_startup.Checked = (rkApp.GetValue(this.regKey) == null); 

     if (rkApp.GetValue(this.regKey) == null) 
     { 
      rkApp.SetValue(this.regKey, appPath); 
     } 
     else 
     { 
      rkApp.DeleteValue(this.regKey, false); 
     } 
    }; 
1

응용 프로그램 시작시 생성 된 레지스트리 키를 만들려면 menu_startup_Click 메서드를 생성자에서 호출해야합니다.

+0

사용자가 제안한 변경 사항은 단순히 레지스트리 키를 만 들지만 다음과 같은 경우는 아닙니다. 1) 컨텍스트 메뉴에 확인 표시를 추가합니다. 2) 컨텍스트 메뉴를 다시 클릭하면 레지스트리 항목을 제거합니다. - 내가 처음에 있었던 것을 의미합니다. 질문에 언급 된 생성자에서 레지스트리 항목을 만들 수 있습니다. ** 편집 : ** 행'this.menu_startup.Checked = false; '레지스트리 항목이 존재하지 않으면 체크 표시를 제거하려고했습니다. – Aborted

+1

제 편집물을보고 적합한 지 알려주세요. –

+0

감사합니다. 편집을 시도했지만 여전히 작동하지 않습니다. 유일한 변경 사항은 레지스트리 항목이 존재하는 경우 체크 표시가있는 것입니다. 이는 우리가 올바른 방향에 있음을 의미합니다. – Aborted

관련 문제