开发者

how to insert any value to registry using C#?

开发者 https://www.devze.com 2023-01-29 22:42 出处:网络
I have to insert this to the registry: [HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows CE Services\\AutoStartOnConnect]

I have to insert this to the registry:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows CE Services\AutoStartOnConnect]
"Auto开发者_运维技巧Run"="d:\\MyFolder\\MyProgram.exe"

How would I do this in C#?


Something like this:

string name = @"SOFTWARE\Microsoft\Windows CE Services\AutoStartOnConnect";
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(name, true))
{
    if (key == null)
    {
        // Whatever you want to do if the key isn't found
    }
    else
    {
        key.SetValue("AutoRun", @"d:\MyFolder\MyProgram.exe");
    }
}

If you use CreateSubKey instead of OpenSubKey, that will create it if it doesn't already exist (or open it for write otherwise) - but I suspect that in most cases, if the key doesn't exist then that indicates the rest of the system isn't in an appropriate state for your app.


You could use the Registry class:

var path = @"Software\Microsoft\Windows CE Services\AutoStartOnConnect";
using (var key = Registry.LocalMachine.OpenSubKey(path, true))
{
    if (key != null)
    {
        key.SetValue("AutoRun", @"d:\MyFolder\MyProgram.exe");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消