Writing to registry in a C# application

前端 未结 5 1026
离开以前
离开以前 2020-12-04 14:17

I\'m trying to write to the registry using my C# app.

I\'m using the answer given here: Writing values to the registry with C#

However for some reason the ke

5条回答
  •  孤街浪徒
    2020-12-04 14:51

    The problem is you don't have enough privileges. Here is a way that works for my:

    RegistryKey myKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    myKey = myKey.OpenSubKey(subkey, RegistryKeyPermissionCheck.ReadWriteSubTree, RegistryRights.FullControl);
    
    if (myKey != null)
    {
        myKey.SetValue("DefaultPrinterId", ldiPrinters[e.RowIndex].id, RegistryValueKind.String);
        myKey.Close();
    }
    

    With RegistryKey.OpenBaseKey you open the correct registry, because when you don't have permissions the registry that you write, it does in another location.

提交回复
热议问题