zoukankan      html  css  js  c++  java
  • 【娱乐】给你的电脑检查兼容性,并获取你的电脑上安装的软件

    最近大家可好。中国又内讧了。

    最近听说腾讯经常扫描用户电脑,获取用户安装软件的信息。所以,我就开始想想腾讯是怎么实现的。按照360的说法,腾讯是通过扫描桌面快捷方式的方式来实现的。

    我觉得腾讯很脑残,为什么要扫描桌面呢?直接用我的方法,保证不会扫描硬盘也能获取安装列表。开个玩笑,其实,聪明的人都知道360想干什么。

    还有说明一下,我对C#不是很熟悉,只是稍微了解一点。本来想用C++写的,但是,博客园的同志据说是专注.net 技术,所以,就用C#吧。

    代码
    using System;
    using System.Diagnostics;
    using Microsoft.Win32;
    using System.Collections.Generic;

    namespace Compatibility
    {

        
    /// <summary>
        
    /// Check Compatibility
        
    /// </summary>
        
    ///
        class Check
        {

            
    private const string SOFT1 = "qq";
            
    private const string SOFT2 = "360";
            
    private static List<SoftWareItem> sofewareList = new List<SoftWareItem>();

            
    public static void Main()
            {
                
    if (HasProcess(SOFT1) && HasProcess(SOFT2))
                {
                    Console.WriteLine(
    "你的电脑同时运行了QQ 和 360 建议卸载其中一个。");
                }
                
    else
                {
                    Console.WriteLine(
    "你的电脑的兼容性良好。\n");
                }

                Console.WriteLine(
    "我顺便扫描了一下你的电脑,看看你自己安装的软件列表吧。");
                Console.WriteLine(
    "\n软件列表:\n");


                List
    <SoftWareItem> softwareList = GetInstalledSoftware();
                
    foreach (SoftWareItem software in softwareList)
                {
                    Console.WriteLine(
    "软件名称: " + software.Name);
                    Console.WriteLine(
    "安装路径: " + software.Path);
                    Console.WriteLine(
    "---------------------------\n\n");
                }
            }

            
    /// <summary>
            
    /// check if has the process 
            
    /// </summary>
            static bool HasProcess(string name)
            {
                
    foreach (Process clsProcess in Process.GetProcesses())
                {
                    
    if (clsProcess.ProcessName.ToLower().Contains(name))
                    {
                        
    return true;
                    }
                }
                
    return false;
            }

            
    /// <summary>
            
    /// Gets a list of installed software and, if known, the software's install path.
            
    /// </summary>
            
    /// <returns></returns>
            static List<SoftWareItem> GetInstalledSoftware()
            {
                
    //query once
                if (sofewareList.Count > 0return sofewareList;

                
    //Declare the string to hold the list:

                
    //The registry key:
                string SoftwareKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall";
                
    using (RegistryKey rk = Registry.LocalMachine.OpenSubKey(SoftwareKey))
                {
                    
    //Let's go through the registry keys and get the info we need:
                    foreach (string skName in rk.GetSubKeyNames())
                    {
                        
    using (RegistryKey sk = rk.OpenSubKey(skName))
                        {
                            
    try
                            {
                                SoftWareItem software 
    = new SoftWareItem();
                                
    //If the key has value, continue, if not, skip it:
                                if (!(sk.GetValue("DisplayName"== null))
                                {
                                    
    //Is the install location known?
                                    software.Name = sk.GetValue("DisplayName").ToString();
                                    
    if (!(sk.GetValue("InstallLocation"== null)) software.Path = sk.GetValue("InstallLocation").ToString();
                                    sofewareList.Add(software);
                                }
                            }
                            
    catch (Exception ex)
                            {
                                
    //No, that exception is not getting away... :P
                                Console.WriteLine(ex.Message);
                            }
                        }
                    }
                }
                sofewareList.Sort(
                
    delegate(SoftWareItem left, SoftWareItem right)
                {
                    
    return left.Name.CompareTo(right.Name);
                }
                );
                
    return sofewareList;
            }
        }

        
    /// <summary>
        
    /// soft ware item
        
    /// </summary>
        class SoftWareItem
        {
            
    private string name = "unknow";
            
    private string path = "Install path unknow";

            
    public string Name
            {
                
    get
                {
                    
    return name;
                }
                
    set
                {
                    
    if (value.Length == 0)
                    {
                        name 
    = "unknow";
                    }
                    
    else
                    {
                        name 
    = value;
                    }
                }
            }

            
    public string Path
            {
                
    get
                {
                    
    return path;
                }

                
    set
                {
                    
    if (value.Length == 0)
                    {
                        path 
    = "Install path unknow";
                    }
                    
    else
                    {
                        path 
    = value;
                    }
                }
            }
        }
    }
  • 相关阅读:
    核心API的使用(给定一个字符串,统计每个字符出现的次数)
    将博客搬至CSDN
    [DEBUG] python写文件时print漏掉整行数据
    [DEBUG] pyinstaller打包-命令行报错 pyinstaller failed to execute script 脚本名
    [DEBUG] springboot结合freemaker和js实现页面跳转和传值-踩坑记录
    724. 寻找数组的中心索引
    1010. 总持续时间可被 60 整除的歌曲
    27.移除元素
    [tensorflow] 入门day1-数据整理与展示
    [tensorflow] 安装
  • 原文地址:https://www.cnblogs.com/niniwzw/p/1869463.html
Copyright © 2011-2022 走看看