2014-02-13 81 views
0

我会直截了当地说我在网上找到了这个代码,因此不是我自己的。如果我输入如程序和功能中所示的程序名称,但是例如,我想键入Mozilla Firefox并找到已安装的Mozilla Firefox 26.0(x86 en-US),则此功能完美。我多次尝试使用.substring和.contains来检查两个字符串,但每次都会导致程序只能锁定。任何帮助表示赞赏。已安装的应用程序名称verus搜索名称

仅供注释: 我使用的是读入的txt文件中的程序列表,以检查已安装的应用程序。 我在设置显示名称后立即运行了一个消息框,其中几个消息框显示为空白。我试图限制它与string.length不是0和长度等于或大于来自txt文件的字符串,但仍然锁定程序。

代码:

private static bool IsAppInstalled(string p_machineName, string p_name) 
{ 
    string keyName; 

    // search in: CurrentUser 
    keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
    if (ExistsInRemoteSubKey(p_machineName, RegistryHive.CurrentUser, keyName, "DisplayName", p_name) == true) 
    { 
     return true; 
    } 

    // search in: LocalMachine_32 
    keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
    if (ExistsInRemoteSubKey(p_machineName, RegistryHive.LocalMachine, keyName, "DisplayName", p_name) == true) 
    { 
     return true; 
    } 

    // search in: LocalMachine_64 
    keyName = @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"; 
    if (ExistsInRemoteSubKey(p_machineName, RegistryHive.LocalMachine, keyName, "DisplayName", p_name) == true) 
    { 
     return true; 
    } 

    return false; 
} 
private static bool ExistsInRemoteSubKey(string p_machineName, RegistryHive p_hive, string p_subKeyName, string p_attributeName, string p_name) 
{ 
    RegistryKey subkey; 
    string displayName; 

    using (RegistryKey regHive = RegistryKey.OpenRemoteBaseKey(p_hive, p_machineName)) 
    { 
     using (RegistryKey regKey = regHive.OpenSubKey(p_subKeyName)) 
     { 
      if (regKey != null) 
      { 
       foreach (string kn in regKey.GetSubKeyNames()) 
       { 
        using (subkey = regKey.OpenSubKey(kn)) 
        { 
         displayName = subkey.GetValue(p_attributeName) as string; 
         MessageBox.Show(displayName); 
         if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true) // key found! 
         { 
          return true; 
         } 
        } 
       } 
      } 
     } 
    } 
    return false; 
} 

我曾尝试过许多不同的事情列出(或记住)...如果有帮助,这是调用它的大部分代码的其余部分:

private void Button_Click(object sender, EventArgs e) 
{ 
    string[] lines = new string[250]; 
    string msg = ""; 
    string path = System.Windows.Forms.Application.StartupPath; 
    if (blah.Checked) 
    { 
     try 
     { 
      StreamReader filePick = new StreamReader(@path + "\\blah.txt"); 
      int counter = 0; 
      while ((lines[counter] = filePick.ReadLine()) != null) 
      { 
       counter++; 
      } 
      filePick.Close(); 
     } 
     catch (Exception ex) 
     { 
      msg += ex.Message; 
     } 
    } 
    else if (blah2.Checked) 
    { 
     try 
     { 
      StreamReader filePick = new StreamReader(@path + "\\blah2.txt"); 
      int counter = 0; 
      while ((lines[counter] = filePick.ReadLine()) != null) 
      { 
       counter++; 
      } 
      filePick.Close(); 
     } 
     catch (Exception ex) 
     { 
      msg += ex.Message; 
     } 
    } 

    string MACHINE_NAME = System.Environment.MachineName; 
    int counter2 = 0; 
    string APPLICATION_NAME = ""; 
    string filename = ""; 
    while (lines[counter2] != null) 
    { 
     APPLICATION_NAME = lines[counter2]; 
     try 
     { 
      bool isAppInstalled = IsAppInstalled(MACHINE_NAME, APPLICATION_NAME); 
      if (isAppInstalled == true) 
      { 
       appsNeedAttention = true; 
       msg += APPLICATION_NAME + " is still installed."; 
      } 
      counter2++; 
     } 
     catch (Exception ex) 
     { 
      msg += ex.Message; 
     } 
    } 

    if (blah.Checked == true) 
    { 
     filename = "blah.txt"; 
    } 
    else if (blah2.Checked == true) 
    { 
     filename = "blah2.txt"; 
    } 
    if (counter2 == 0 && File.Exists(filename) == true) 
    { 
     msg = "There are no programs listed in the file."; 
    } 
    if (msg != "") 
    { 
     MessageBox.Show(msg, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
    } 
} 
+0

如果您告诉我们您的输入,期望的输出是什么,实际输出以及发生的任何异常的详细信息,它都会有所帮助。 – mason

+0

输入只是一个带有程序名称的txt文件(每个程序名称都在一个单独的行中),输出只是一个msgbox,用于说明实际安装了哪个程序。 – user3304246

+0

,尽管没有匹配,在Mozilla Firefox中显示的消息框吗?如果没有显示,那么问题不在于匹配逻辑,而是在获取已安装的应用程序列表的逻辑上,而不是 – har07

回答

0

尝试改变这一部分:

if (p_name.Equals(displayName, StringComparison.OrdinalIgnoreCase) == true) 
{ 
    return true; 
} 

要这样:

//check null to avoid error 
if (!string.IsNullOrEmpty(displayName)) 
{ 
    //convert both string to lower case to ignore case difference 
    //and use contains to match partially 
    if (displayName.ToLower().Contains(p_name.ToLower())) 
    { 
     return true; 
    } 
} 
相关问题