开发者

C# Registry issue

开发者 https://www.devze.com 2023-04-12 15:29 出处:网络
This is running in a background worker The issue appears to be with the registry check if the key FS is missing it errors out.

This is running in a background worker The issue appears to be with the registry check if the key FS is missing it errors out. I'm doing this check just in case someone deletes it even though the installer adds the values. Also wondering why message boxes aren't working

    //I DONT EVEN GET TO THIS MESSAGE BOX BEFORE THE ERROR
    //BUT THE CATCH IS TRIGGERED FROM THE TRY BLOCK
    MessageBox.Show("Entered Version Check");
    try
    {
        //DONT GET TO SEE THIS MESSAGE BOX
        MessageBox.Show("Entered try");
        DirectoryInfo Temp = new DirectoryInfo(Path.GetTempPath());
        RegistryKey rk = Registry.LocalMachine.OpenSubKey("Software\\My Application Here");
        string val;
        if (rk != null)
            {
                val = rk.GetValue("FS").ToString();
                if (val == null)
                {
                    MessageBox.Show("It appears that you do not have a File Server IP address configured" + Environm开发者_Go百科ent.NewLine + "Please login to the admin tools and set it", "No File Server Set", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }


            val = rk.GetValue("FS").ToString();

That's going to bomb if GetValue() returns null. First check for null before you try ToString(). And add a check in the BGW's RunWorkerCompleted event handler so you can verify e.Error. And don't display message boxes in a worker thread, odds are good that they'll disappear behind the window of another app, including your own.


The most common issue like this I've seen is related to the 64bit registry redirection. Are you running on a 64bit OS, with your application compiled as 32bit? If so, the registry calls will be redirected.

Either look under the HKLM\SOFTWARE\Wow6432Node key for your key, or compile your application as 64bit.

0

精彩评论

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

关注公众号