如果C#代码中出现异常,如何执行剩余的代码


RegistryKey key10 = Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesDiagTrack", true);
RegistryKey key11 = Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesdmwappushservice", true);
RegistryKey key12 = Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesdiagsvc", true);
RegistryKey key14 = Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesdiagnosticshub.standardcollector.service", true);
RegistryKey key15 = Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesWdiServiceHost", true);
RegistryKey key16 = Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesWdiSystemHost", 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();
}

我想做的是,当编译器尝试try方法中的代码时,如果发现任何异常,那么它应该执行try方法的其余代码,而不是忽略所有代码。

C#中没有On Error Resume Next。相反:让每一件作品都不失败。例如,你可以做:

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(@"SYSTEMCurrentControlSetServicesDiagTrack");
TryDoTheThing(@"SYSTEMCurrentControlSetServicesdmwappushservice");
TryDoTheThing(@"SYSTEMCurrentControlSetServicesdiagsvc");
TryDoTheThing(@"SYSTEMCurrentControlSetServicesdiagnosticshub.standardcollector.service");
TryDoTheThing(@"SYSTEMCurrentControlSetServicesWdiServiceHost");
TryDoTheThing(@"SYSTEMCurrentControlSetServicesWdiSystemHost");

尝试以下操作:

string[] keys = {
@"SYSTEMCurrentControlSetServicesDiagTrack",
@"SYSTEMCurrentControlSetServicesdmwappushservice",
@"SYSTEMCurrentControlSetServicesdiagsvc",
@"SYSTEMCurrentControlSetServicesdiagnosticshub.standardcollector.service",
@"SYSTEMCurrentControlSetServicesWdiServiceHost",
@"SYSTEMCurrentControlSetServicesWdiSystemHost"
};
foreach (string key in keys)
{
RegistryKey newKey = Registry.LocalMachine.OpenSubKey(key, true);
try
{
newKey.SetValue("Start", 4, RegistryValueKind.DWord);
}
catch (Exception)
{
}
}

您可以将密钥存储到一个数组中,然后逐个迭代

RegistryKey[] keys = new RegistryKey[]
{
Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesDiagTrack", true),
Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesdmwappushservice", true),
Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesdiagsvc", true),
Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesdiagnosticshub.standardcollector.service", true),
Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesWdiServiceHost", true),
Registry.LocalMachine.OpenSubKey(@"SYSTEMCurrentControlSetServicesWdiSystemHost", true)
};
for (int i = 0; i < keys.Length; i++)
{
try
{
keys[i].SetValue("Start", 4, RegistryValueKind.DWord);
}
catch (Exception exception)
{
}
}

读取

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));

}

最新更新