Wednesday, 2 September 2015

.Net Application with "Start with Windows" option




Adding a Start-With-Windows option can become quite a tedious task for those new to developing for windows. The solution rather, is very simple. We use a RegistryKey object (part of Microsoft.Win32 namespace) to add an entry to the Windows Registry for startup. Then we connect it to a check-box using an event trigger.

1. The program checks the state of startup at application-load.
2. The check-box updates the start-up state as you check or un-check it.

Code written in C++, but you can easily convert it.

Implementation (Application Load Event):

void Load_Event(){
    this->BootCheckBox->Checked = this->BootRun;
}

Implementation (Checkbox Check-Changed Event):

System::Void BootCheckBox_CheckedChanged(System::Object^  sender, System::EventArgs^  e) {
    this->BootRun = this->BootCheckBox->Checked;
}

Definition:

property bool BootRun{
    bool get(void){
        bool r = false;
        if( Registry::CurrentUser->OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)
            ->GetValue("CSX_TrayCut") ) r = true;
        return r;
    }
    void set(bool i){
        if( i ) Registry::CurrentUser->OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)
            ->SetValue("CSX_TrayCut",
            Application::ExecutablePath->ToString());
        else Registry::CurrentUser->OpenSubKey
            ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)
            ->DeleteValue("CSX_TrayCut", false );
    }
}


No comments:

Post a Comment