简体   繁体   English

改善现有代码段所需的帮助

[英]Help needed in improving the existing code snippet

I have written a code snippet which i definitely feel its not written a correct way. 我已经写了一个代码片段,我肯定觉得这不是正确的方式。 My method must return a string based on the version of the Adobe Installed in my machine. 我的方法必须返回一个字符串,该字符串基于计算机中安装的Adobe的版本。

private string GetRegistryKeyPathForX()
        {
            string sPath = string.Empty;
            RegistryKey adobe = Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Adobe");
            if (adobe != null)
            {
                RegistryKey acroRead = adobe.OpenSubKey("Adobe Acrobat");
                if (acroRead != null)
                {
                    string[] acroReadVersions = acroRead.GetSubKeyNames();
                    //The following version(s) of Acrobat Reader are installed
                    foreach (string versionNumber in acroReadVersions)
                    {
                        switch(versionNumber)
                        {
                            case "6.0": sPath = "Software\\Adobe\\Acrobat Reader";
                                        return sPath;  //Improve here
                                        break;

                            case "7.0":
                                        sPath = "";
                                        return sPath;   //Improve here
                                        break;
                            case "8.0": 
                                        sPath = "";
                                        return sPath;   //Improve here
                                        break;
                            case "9.0" :
                                        sPath = "Software\\Adobe\\Acrobat Reader";
                                        return sPath;   //Improve here
                                        break;
                            default: sPath = "ERR_KEY";
                                            break;
                        }  
                    }
                }
            }
        return sPath;
    }

Is there a way so that i can return the string in one shot instead of assigning it in each case statement.? 有没有一种方法可以让我一次返回字符串,而不是在每个case语句中分配它。

If you just optimize the switch, you could write: 如果仅优化开关,则可以编写:

switch(versionNumber)
{
   case "6.0": 
   case "9.0": 
      return "Software\\Adobe\\Acrobat Reader";
  case "7.0":
  case "8.0": 
      return string.Empty;
  default: 
      return "ERR_KEY";
}

However, I prefer using maps instead of switches (especially if you need this logic in more than one place), so you could do: 但是,我更喜欢使用地图而不是开关(尤其是如果您需要在多个地方使用此逻辑),因此可以执行以下操作:

Dictionary<string, string> versionPaths = new Dictionary<string, string>();
versionPaths.Add("6.0","Software\\Adobe\\Acrobat Reader");
versionPaths.Add("7.0",string.empty);
....
if (versionPaths.ContainsKey(versionNumber))
  return versionPaths[versionNumber];
else
  return "ERR_KEY";

Yes: you don't need either the assignment or the break statement: 是的:您不需要赋值或break语句:

case "6.0": return "Software\\Adobe\\Acrobat Reader";

If you can get a copy of ReSharper it can really help with this sort of thing: in this case, it would highlight the unused code break statement in grey, and it would offer to automatically remove the assignment. 如果您可以获得ReSharper的副本,则可以真正帮助解决此类问题:在这种情况下,它将以灰色突出显示未使用的代码break语句,并提供自动删除分配的功能。

No need to write break; 无需写break; since it is returning values from there 因为它从那里返回值

switch(versionNumber)
 {
     case "6.0": return "Software\\Adobe\\Acrobat Reader";  
     .
     .
     .
 }

OR return after switch statement 在switch语句后返回

switch(versionNumber)
 {
     case "6.0": sPath = "Software\\Adobe\\Acrobat Reader";          
         break;
     .
     .
     .
 }
 return sPath;

You can replace the loop and the switch with this: 您可以将循环和switch替换为:

if (acroRead.GetSubKeyNames().ToList().FindAll(versionNumber => versionNumber.Equals("6.0") || versionNumber.Equals("9.0")).Count > 0)
   return "Software\\Adobe\\Acrobat Reader";

Edit: .NET 2 version: 编辑:.NET 2版本:

if (Array.FindAll<string>(acroRead.GetSubKeyNames(), delegate(string versionNumber) { return versionNumber.Equals("6.0") || versionNumber.Equals("9.0"); }).Length > 0)
private string GetRegistryKeyPathForX()
{
    var adobe = Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Adobe");
    if (adobe == null)
    {
        return string.Empty;
    }

    RegistryKey acroRead = adobe.OpenSubKey("Adobe Acrobat");
    if (acroRead == null)
    {
        return string.Empty;
    }

    string[] acroReadVersions = acroRead.GetSubKeyNames();

    //The following version(s) of Acrobat Reader are installed
    foreach (string versionNumber in acroReadVersions)
    {
        switch (versionNumber)
        {
            case "6.0": return "Software\\Adobe\\Acrobat Reader";
            case "7.0": return "";
            case "8.0": return "";
            case "9.0": return "Software\\Adobe\\Acrobat Reader";
            default:    break;
        }
    }

    return "ERR_KEY";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM