Home > Software engineering >  How to execute the remaining code if there is any Exception in C# code?
How to execute the remaining code if there is any Exception in C# code?

Time:10-04

RegistryKey key10 = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\DiagTrack", true);
        RegistryKey key11 = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\dmwappushservice", true);
        RegistryKey key12 = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\diagsvc", true);
        RegistryKey key14 = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\diagnosticshub.standardcollector.service", true);
        RegistryKey key15 = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\WdiServiceHost", true);
        RegistryKey key16 = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\WdiSystemHost", true);
        try
        {
            key10.SetValue("Start", 4, RegistryValueKind.DWord);
            key11.SetValue("Start", 4, RegistryValueKind.DWord);
            key12.SetValue("Start", 4, RegistryValueKind.DWord);
            key14.SetValue("Start", 4, RegistryValueKind.DWord);
            key15.SetValue("Start", 4, RegistryValueKind.DWord);
            key16.SetValue("Start", 4, RegistryValueKind.DWord);

        }

        catch(Exception)
        {

        }

        finally
        {
            key10?.Dispose();
            key11?.Dispose();
            key12?.Dispose();
            key14?.Dispose();
            key15?.Dispose();
            key16?.Dispose();
        }

What I want to do here is that while the compiler tries the code in the try method and if it finds any exception, then it should execute the rest of the code in the try method instead of just ignoring all of the code.

CodePudding user response:

There is no On Error Resume Next in C#. Instead: make each individual piece not fail. For example, you could do:

static bool TryDoTheThing(string path) {
    try {
        using var key = Registry.LocalMachine.OpenSubKey(path, true);
        key.SetValue("Start", 4, RegistryValueKind.DWord);
        return true;
    } catch {
        return false;
    }
}
TryDoTheThing(@"SYSTEM\CurrentControlSet\Services\DiagTrack");
TryDoTheThing(@"SYSTEM\CurrentControlSet\Services\dmwappushservice");
TryDoTheThing(@"SYSTEM\CurrentControlSet\Services\diagsvc");
TryDoTheThing(@"SYSTEM\CurrentControlSet\Services\diagnosticshub.standardcollector.service");
TryDoTheThing(@"SYSTEM\CurrentControlSet\Services\WdiServiceHost");
TryDoTheThing(@"SYSTEM\CurrentControlSet\Services\WdiSystemHost");

CodePudding user response:

Try following :

            string[] keys = {
                 @"SYSTEM\CurrentControlSet\Services\DiagTrack",
                 @"SYSTEM\CurrentControlSet\Services\dmwappushservice",
                 @"SYSTEM\CurrentControlSet\Services\diagsvc",
                 @"SYSTEM\CurrentControlSet\Services\diagnosticshub.standardcollector.service",
                 @"SYSTEM\CurrentControlSet\Services\WdiServiceHost",
                 @"SYSTEM\CurrentControlSet\Services\WdiSystemHost"
                            };

            foreach (string key in keys)
            {
                RegistryKey newKey = Registry.LocalMachine.OpenSubKey(key, true);
                try
                {
                    newKey.SetValue("Start", 4, RegistryValueKind.DWord);
                }
                catch (Exception)
                {
                }
            }

CodePudding user response:

You can store the keys into an array then iterate one by one

RegistryKey[] keys = new RegistryKey[]
{
    Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\DiagTrack", true),
    Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\dmwappushservice", true),
    Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\diagsvc", true),
    Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\diagnosticshub.standardcollector.service", true),
    Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\WdiServiceHost", true),
    Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\WdiSystemHost", true)
};

for (int i = 0; i < keys.Length; i  )
{
    try
    {
        keys[i].SetValue("Start", 4, RegistryValueKind.DWord);
    }
    catch (Exception exception)
    {

    }
}

To Read

           Dictionary<string, object> dict = new Dictionary<string, object>();

            foreach (string key in keys)
            {
                RegistryKey rkey =  Registry.LocalMachine.OpenSubKey(key, true);
                dict.Add(key, rkey.GetValue("Start", RegistryValueKind.DWord));
                
            }
  •  Tags:  
  • c#
  • Related