Saving registry changes permanently in c#

  • Hello,


    I'm trying to update the TcpIp settings in HKEY_LOCAL_MACHINE\Comm\ETHNETA1\Parms\TcpIp. After writing, I can see that the changes have been written but after reboot the changes are gone again. I do this:


    OpenSubKey, SetValue, Flush(), Close()


    Flush, to my understanding, should make the changes permament but that doesn't seem to work.


    So, the question is: what do I have to do to to make the registry changes permanent?


    Regards,
    Dag Hovden

  • Well, neither Flush nor Close works. Here's my code:


    RegistryKey rkHKLM = Registry.LocalMachine;
    RegistryKey rkIP = rkHKLM.OpenSubKey("Comm\\ETHNETA1\\Parms\\TcpIp", true);
    rkIP.SetValue("EnableDHCP", 0);
    rkIP.SetValue("IpAddress",sIP); // Typical value: 192.168.0.58
    rkIP.SetValue("Subnetmask",sSub);
    rkHKLM.Flush();
    rkHKLM.Close();
    MessageBox.Show("Saved IP=" + sIP + ", subnet=" + sSub);


    After this, I can see that the registry has been correctly changed but if I then issue 'reboot hardware' in DCUterm, after reboot the old settings are back.


    I have also seen that there is another key that defines EnableDHCP, "Comm\\TcpIp\\Parms". What is the function of that one? Can I ignore it or do both need to set both EnableDHCP values?


    ncducfg, is that a program that is always available (I have a PicoCom3)? And how do I use from C# - your example is a C++/Win32 example.

  • Hello you don't have to touch the key ""Comm\\TcpIp\\Parms".
    I don't know what's wrong in your code.
    I would prefer "ndcucfg" with commandline. I think you can use "StartProcess" or DLL import of "CreateProcess" for this purpose.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • I'm trying this



    System.Diagnostics.Process.Start("ndcucfg.exe","-c 'reg save'");


    but it raises an exception, probably ndcucfg cannot be found. Running from the command prompt works fine.
    I think I need to specify the complete path to ndcucfg.exe but I cannot find the executable anywhere on my PicoCom3.

  • This works


    System.Diagnostics.Process.Start("\\Windows\\cmd.exe","ndcucfg -c 'reg save'");


    I think that rkHKLM.Flush(); saves to RAM but the above is needed to flash it and make the changes permanent. Would be nice if there was a neater way then running ndcucfg through cmd.exe.

  • Hello,
    yes it not very nice to call "ndcucfg" via "cmd" because you see the command shell for a short time. In VC++ i did following and it works:

    Code
    1. BOOL bRes = CreateProcess(_T("ndcucfg"), _T("-c \"touch calib\""), NULL , NULL, 0, 0, NULL, NULL, NULL, NULL);


    So you should try

    Code
    1. System.Diagnostics.Process.Start("ndcucfg","-c \"reg save\"");


    PS:
    1. ndcucfg is located under \windows. Maybe file attribute is "hidden", so you have the fit explorers display options.
    2. Registry changes are always done in RAM and disappear after power off, becaues we have a hive based registry "Flush" should save registry into flash memory.

  • Hello again,
    i found several entries in this forum which deal about the same subject. Indeed it seems to be a bug that registry saving does not work, but here is a smart workaround: http://www.forum.fs-net.de/vie…php?f=2&t=163&hilit=Flush.