Browse Source

添加获取控制面板软件信息工具类

yuzhengyang 8 years ago
parent
commit
c8c95661f6

+ 47 - 6
Fork.Net/Test/Y.Test/Views/TestComputerInfoForm.cs

@@ -1,4 +1,5 @@
-using System;
+using Microsoft.Win32;
+using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.ComponentModel;
 using System.Data;
 using System.Data;
@@ -16,9 +17,17 @@ namespace Y.Test.Views
         {
         {
             InitializeComponent();
             InitializeComponent();
         }
         }
-
         private void TestComputerInfoForm_Load(object sender, EventArgs e)
         private void TestComputerInfoForm_Load(object sender, EventArgs e)
         {
         {
+        }
+        private void Print(string s)
+        {
+            textBox1.AppendText(s);
+            textBox1.AppendText(Environment.NewLine);
+        }
+
+        private void GetHardInfo()
+        {
             Print("UserName: " + ComputerInfoTool.UserName());
             Print("UserName: " + ComputerInfoTool.UserName());
             Print("登录用户名: " + ComputerInfoTool.UserName2());
             Print("登录用户名: " + ComputerInfoTool.UserName2());
 
 
@@ -39,17 +48,49 @@ namespace Y.Test.Views
                 Print("硬盘 序列号 " + x.Item1 + " 型号 " + x.Item2);
                 Print("硬盘 序列号 " + x.Item1 + " 型号 " + x.Item2);
             });
             });
             var osinfo = ComputerInfoTool.OsInfo();
             var osinfo = ComputerInfoTool.OsInfo();
-            Print("操作系统: " + osinfo.Item1 + " " + osinfo.Item2+" "+osinfo.Item3);
+            Print("操作系统: " + osinfo.Item1 + " " + osinfo.Item2 + " " + osinfo.Item3);
             Print("系统类型: " + ComputerInfoTool.SystemType());
             Print("系统类型: " + ComputerInfoTool.SystemType());
             Print("计算机名: " + ComputerInfoTool.MachineName());
             Print("计算机名: " + ComputerInfoTool.MachineName());
             Print("系统所有用户名: " + string.Join(",", ComputerInfoTool.UserNames()));
             Print("系统所有用户名: " + string.Join(",", ComputerInfoTool.UserNames()));
             var boardinfo = ComputerInfoTool.BoardInfo();
             var boardinfo = ComputerInfoTool.BoardInfo();
             Print("主板 制造商 " + boardinfo.Item1 + " 型号 " + boardinfo.Item2 + " 序列号 " + boardinfo.Item3);
             Print("主板 制造商 " + boardinfo.Item1 + " 型号 " + boardinfo.Item2 + " 序列号 " + boardinfo.Item3);
         }
         }
-        private void Print(string s)
+
+        private void GetSoftInfo()
         {
         {
-            textBox1.AppendText(s);
-            textBox1.AppendText(Environment.NewLine);
+            string temp = null, tempType = null;
+            object displayName = null, uninstallString = null, releaseType = null;
+            RegistryKey currentKey = null;
+            int softNum = 0;
+            RegistryKey pregkey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall");//获取指定路径下的键
+            try
+            {
+                foreach (string item in pregkey.GetSubKeyNames())               //循环所有子键
+                {
+                    currentKey = pregkey.OpenSubKey(item);
+                    displayName = currentKey.GetValue("DisplayName");           //获取显示名称
+                    uninstallString = currentKey.GetValue("UninstallString");   //获取卸载字符串路径
+                    releaseType = currentKey.GetValue("ReleaseType");           //发行类型,值是Security Update为安全更新,Update为更新
+                    bool isSecurityUpdate = false;
+                    if (releaseType != null)
+                    {
+                        tempType = releaseType.ToString();
+                        if (tempType == "Security Update" || tempType == "Update")
+                            isSecurityUpdate = true;
+                    }
+                    if (!isSecurityUpdate && displayName != null && uninstallString != null)
+                    {
+                        softNum++;
+                        temp += displayName.ToString() + Environment.NewLine;
+                    }
+                }
+            }
+            catch (Exception E)
+            {
+                MessageBox.Show(E.Message.ToString());
+            }
+            richTextBox1.Text = "您本机安装了" + softNum.ToString() + "个" + Environment.NewLine + temp;
+            pregkey.Close();
         }
         }
     }
     }
 }
 }

+ 18 - 0
Fork.Net/Y.Utils/SoftwareUtils/SoftwareModel.cs

@@ -0,0 +1,18 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Y.Utils.SoftwareUtils
+{
+    public class SoftwareModel
+    {
+        public string Name { get; set; }
+        public string Version { get; set; }
+        public string Publisher { get; set; }
+        public string HelpLink { get; set; }
+        public string URLInfoAbout { get; set; }
+        public int EstimatedSize { get; set; }
+        public DateTime InstallDate { get; set; }
+    }
+}

+ 148 - 0
Fork.Net/Y.Utils/SoftwareUtils/SoftwareTool.cs

@@ -0,0 +1,148 @@
+using Microsoft.Win32;
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.IO;
+using System.Linq;
+using System.Text;
+using Y.Utils.DataUtils.Collections;
+
+namespace Y.Utils.SoftwareUtils
+{
+    public class SoftwareTool
+    {
+        /// <summary>
+        /// 存在控制面板
+        /// </summary>
+        /// <param name="name"></param>
+        /// <returns></returns>
+        public static bool ExistControl(string name)
+        {
+            if (GetControlList().Contains(name))
+                return true;
+            return false;
+        }
+        public static bool ExistControl(string[] names)
+        {
+            bool flag = false;
+            if (!ListTool.IsNullOrEmpty(names))
+            {
+                foreach (var n in names)
+                {
+                    if (ExistControl(n))
+                        return true;
+                }
+            }
+            return flag;
+        }
+        public static List<string> GetControlList()
+        {
+            List<string> result = new List<string>();
+            result.AddRange(GetControlList(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
+            result.AddRange(GetControlList(Registry.LocalMachine.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
+            result.AddRange(GetControlList(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
+            result.AddRange(GetControlList(Registry.CurrentUser.OpenSubKey("SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall")));
+            return result;
+        }
+        private static List<string> GetControlList(RegistryKey key)
+        {
+            List<string> result = new List<string>();
+            try
+            {
+                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")
+                            {
+                                result.Add(SoftwareName.Trim());
+                            }
+                        }
+                        SubKey?.Close();
+                    }
+                }
+                key?.Close();
+            }
+            catch { }
+            return result;
+        }
+        /// <summary>
+        /// 存在进程
+        /// </summary>
+        /// <param name="name"></param>
+        /// <returns></returns>
+        public static bool ExistProcess(string name)
+        {
+            try
+            {
+                var p = Process.GetProcessesByName(name);
+                if (p != null && p.Length == 0)
+                    return false;
+                else
+                    return true;
+            }
+            catch { }
+            return false;
+        }
+        public static bool ExistProcess(string[] names)
+        {
+            bool flag = false;
+            if (!ListTool.IsNullOrEmpty(names))
+            {
+                foreach (var n in names)
+                {
+                    if (ExistProcess(n))
+                        return true;
+                }
+            }
+            return flag;
+        }
+        /// <summary>
+        /// 存在文件
+        /// </summary>
+        /// <param name="name"></param>
+        /// <returns></returns>
+        public static bool ExistFile(string name)
+        {
+            if (File.Exists(name))
+                return true;
+            return false;
+        }
+        public static bool ExistFile(string[] names)
+        {
+            bool flag = false;
+            if (!ListTool.IsNullOrEmpty(names))
+            {
+                foreach (var n in names)
+                {
+                    if (ExistFile(n))
+                        return true;
+                }
+            }
+            return flag;
+        }
+        /// <summary>
+        /// 存在注册表项
+        /// </summary>
+        /// <param name="item"></param>
+        /// <param name="key"></param>
+        /// <returns></returns>
+        public static bool ExistRegist(string item, string key)
+        {
+            try
+            {
+                object obj = Registry.GetValue(item, key, null);
+                if (obj != null)
+                    return true;
+            }
+            catch { }
+            return false;
+        }
+    }
+}

+ 1 - 0
Fork.Net/Y.Utils/WindowsUtils/InfoUtils/SoftwareTool.cs

@@ -9,6 +9,7 @@ using Y.Utils.DataUtils.Collections;
 
 
 namespace Y.Utils.WindowsUtils.InfoUtils
 namespace Y.Utils.WindowsUtils.InfoUtils
 {
 {
+    [Obsolete]
     public class SoftwareTool
     public class SoftwareTool
     {
     {
         /// <summary>
         /// <summary>

+ 2 - 0
Fork.Net/Y.Utils/Y.Utils.csproj

@@ -86,6 +86,8 @@
     <Compile Include="IOUtils\PathUtils\DirTool.cs" />
     <Compile Include="IOUtils\PathUtils\DirTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileCodeTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileCodeTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileTool.cs" />
     <Compile Include="IOUtils\FileUtils\FileTool.cs" />
+    <Compile Include="SoftwareUtils\SoftwareModel.cs" />
+    <Compile Include="SoftwareUtils\SoftwareTool.cs" />
     <Compile Include="WindowsUtils\APIUtils\FormStyleAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\FormStyleAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\PermissionAPI.cs" />
     <Compile Include="WindowsUtils\APIUtils\PermissionAPI.cs" />
     <Compile Include="WindowsUtils\InfoUtils\ClipboardTool.cs" />
     <Compile Include="WindowsUtils\InfoUtils\ClipboardTool.cs" />