Create key in windows registry without permission in C #

3

My application needs to insert itself into Windows Defender exclusions. Just create a MYapp.exe key on the path {HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Defender\Exclusions\Processes} , but it is returning the message stating that I do not have permission.

I am an administrator user of the machine and my application is running as an administrator but the error is the same.

Windows 8.1 and 10 have a command that can be executed by Power Shell, but Windows 7 does not. Can you help me?

Here is the code I'm using:

RegistryKey localRaiz;
RegistryKey key;

try
{
    string path = @"SOFTWARE\Microsoft\Windows Defender\Exclusions\Processes";
    localRaiz = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);

    key = localRaiz.OpenSubKey(path);
    key.SetValue("MPI.exe", 0, RegistryValueKind.DWord);
    key.Close();
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
    
asked by anonymous 06.06.2017 / 20:40

2 answers

2

You have to open the key in "writable" mode, so just change

key = localRaiz.OpenSubKey(path);

for

key = localRaiz.OpenSubKey(path, true);

Reference: RegistryKey.OpenSubKey Method

    
06.06.2017 / 22:21
1

Run VS as an administrator and your application as an administrator

public static  bool GravarRegistroUsersSoftware(string Subchave, string nomevalor, string valor)
{
   try
   {
       // cria uma referêcnia para a chave de registro Software
       RegistryKey rk = Registry.Users.OpenSubKey(".DEFAULT\Software", true);

       // cria um Subchave como o nome GravaRegistro
       rk = rk.CreateSubKey(Subchave);

       // grava o caminho na SubChave GravaRegistro
       rk.SetValue(nomevalor, valor);

       // fecha a Chave de Restistro registro
       rk.Close();

       return true;
   }

   catch (Exception except)
   {
       UltimoErro = except.Message;
       return false;
   }
}

public static  string LerRegistroUsersSoftware(string Subchave, string nomevalor)
{
   try
   {
       string tmpstr = null;
       // cria uma referêcnia para a chave de registro Software
       RegistryKey rk = Registry.Users.OpenSubKey(".DEFAULT\Software", true);

       // realiza a leitura do registro
       tmpstr = rk.OpenSubKey(Subchave, true).GetValue(nomevalor).ToString();
       return tmpstr;
   }

   catch (Exception except)
   {
       UltimoErro = except.Message;
       return null;
   }
}
    
22.08.2018 / 19:25