2015-02-09 56 views
1

如果我的問題的標題不適合,我很抱歉。C#類和對象:對象包含單個值v0ue

我是編程方面的新手,並試圖編寫一個控制檯應用程序,用於從註冊表中獲取計算機上安裝的所有應用程序。

這是我的代碼。

class Program 
{ 
    static void Main(string[] args) 
    { 
     Console.Title = "Windows App Finder - Finds all the installed apps on the station"; 

     List<AppDetails> apps = new List<AppDetails>(); 
     apps = GetInstalledApps(); 
     ListAllApps(apps); 
     Console.WriteLine("\nPress any key to exit."); 
     Console.ReadLine(); 
    } 

    private static List<AppDetails> GetInstalledApps() 
    { 
     string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
     AppDetails appInfo = new AppDetails(); 
     List<AppDetails> apps = new List<AppDetails>(); 
     using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key)) 
     { 
      foreach (string subkey_name in key.GetSubKeyNames()) 
      { 
       using (RegistryKey subkey = key.OpenSubKey(subkey_name)) 
       { 

        appInfo.appName = (string)subkey.GetValue("DisplayName"); 
        appInfo.publisher = (string)subkey.GetValue("Publisher"); 
        appInfo.appVersion = (string)subkey.GetValue("DisplayVersion"); 
        if (appInfo.appName != null) 
        { 
         apps.Add(appInfo); 
        } 
       } 
      } 
     } 
      return apps; 
    } 
    private static void ListAllApps(List<AppDetails> appDetails) 
    { 
     int i = 0; 
     foreach (AppDetails appDetail in appDetails) 
     { 
      Console.WriteLine(i.ToString() + ". Name: " + appDetail.appName + " Version: " + appDetail.appVersion + " Publisher: " + appDetail.publisher); 
      i++; 
     } 
    } 
} 

public class AppDetails 
{ 
    public string appName { get; set; } 
    public string appVersion {get; set;} 
    public string publisher {get;set;} 
    public DateTime installDate { get; set; } 
} 

}

此代碼返回http://i.stack.imgur.com/tKtNr.png 單一的應用程序名稱重複多次。這段代碼有什麼問題。

在此先感謝

回答

4

你必須實例AppDetails appInfo = new AppDetails();您的foreach循環裏面,否則你將與同一個對象最終得到修改,再而添加到列表中。

只是將實例化移動到循環中。

private static List<AppDetails> GetInstalledApps() 
{ 
    string registry_key = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; 
    List<AppDetails> apps = new List<AppDetails>(); 
    using (Microsoft.Win32.RegistryKey key = Registry.LocalMachine.OpenSubKey(registry_key)) 
    { 
     foreach (string subkey_name in key.GetSubKeyNames()) 
     { 
      using (RegistryKey subkey = key.OpenSubKey(subkey_name)) 
      { 
       AppDetails appInfo = new AppDetails(); //HERE 
       appInfo.appName = (string)subkey.GetValue("DisplayName"); 
       appInfo.publisher = (string)subkey.GetValue("Publisher"); 
       appInfo.appVersion = (string)subkey.GetValue("DisplayVersion"); 
       if (appInfo.appName != null) 
       { 
        apps.Add(appInfo); 
       } 
      } 
     } 
    } 
    return apps; 
} 

既然你不實例化循環內的對象,它被添加到列表多次,並在列表指向相同的所有實例。因此您會看到多次返回相同的值。這將是您迭代中的最後一個值。

+0

謝謝@Habib It did work :) – Dev05 2015-02-09 17:34:02

+0

'Console.WriteLine(i.ToString()+「。Name:」+ appDetail.appName +「Version:」+ appDetail.appVersion +「發佈者:」+ appDetail。發佈者);」 這是部分地返回的值,我的意思是在控制檯我只能從我看到= 20。 如果我給出代碼 「Console.WriteLine(i.ToString()+‘姓名:’+ appDetail .appName);」 我可以從i = 0看到; 任何猜測什麼是錯的? – Dev05 2015-02-09 17:37:55

+0

@ Dev05,你的屏幕可能會滾動並顯示最後的記錄。向上滾動,如果不起作用,則在循環中檢查「i」的值後,放置一個簡單的'Console.ReadLine();'。 '如果(i%10 == 0)',那麼使用'Console.ReadLine' – Habib 2015-02-09 17:42:37