本文實例講述了C#通過windows注冊表獲取軟件清單的方法。分享給大家供大家參考。具體如下:
foreach (string SoftwareName in Object.SoftwareList()){ textBox.Text += SoftwareName + Environment.NewLine;}/////////////////////////////////////////////////////////////////////////// <summary>/// Windows系統獲取軟件列表/// </summary>/// <returns>String [] softwareList</returns>public String [] SoftwareList(){ String[] softwareList = null; //動態數組 ArrayList list = new ArrayList(); try { //打開注冊列表卸載選項 //SOFTWARE/Microsoft/Windows/CurrentVersion/Uninstall RegistryKey Key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Uninstall"); if (Key != null)//如果系統禁止訪問則返回null { foreach (String SubKeyName in Key.GetSubKeyNames()) { //打開對應的軟件名稱 RegistryKey SubKey = Key.OpenSubKey(SubKeyName); if (SubKey != null) { String SoftwareName = SubKey.GetValue("DisplayName", "Nothing").ToString(); //如果沒有取到,則不存入動態數組 if (SoftwareName != "Nothing") { list.Add(SoftwareName); } } } //強制轉換成字符串數組,防止被修改數據溢出 softwareList = (string[])list.ToArray(typeof(string)); } } catch (Exception err) { Console.WriteLine("出錯信息:" + err.ToString()); } return softwareList;}
希望本文所述對大家的C#程序設計有所幫助。
新聞熱點
疑難解答