How to make application lock/unlock on Windows startup?

Asked

Viewed 125 times

2

I want to make an app that will have an option to call the Regedit (Windows Registry) and the user will be able to choose whether to lock or unlock so that the application is booted with Windows.

I’ve searched several sites and even here on Stackoverflow but I couldn’t find a question that really answered my question completely, because they all had code to do only on the user’s machine manually, but I want the user to be able to choose within an option whether it will activate or not for the application to boot together or be locked when starting Windows.

I am making this app in C#,and would like to know how to create it. If anyone can help me,.

1 answer

10


Keep in mind that everything in C# is class-based, so at first glance you should imagine that there is a class for this.

The Registrykey class (Microsoft.Win32) serves exactly to create a registry key as you wish:

//Criando uma subchave nos registros, a string é o diretório padrão:
RegistryKey SeuApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

Now the user can use two methods to create or delete the value of the record, it is up to you to implement:

//Adiciona o valor no registro para iniciação com sistema.
SeuApp.SetValue("NomeApresentacao", Application.ExecutablePath);
//Remove o valor do registro.
SeuApp.DeleteValue("NomeApresentacao");

That one NomeApresentacao should be renamed with the name you want to appear.

Reference: https://msdn.microsoft.com/pt-br/library/microsoft.win32.registrykey(v=vs.110). aspx

  • 1

    Thank you friend, I tested your code and made the appropriate changes to my application and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.