【娱乐】给你的电脑检查兼容性,并获取你的电脑上安装的软件

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

最近听说腾讯经常扫描用户电脑,获取用户安装软件的信息。所以,我就开始想想腾讯是怎么实现的。按照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;
                }
            }
        }
    }
}

 

posted @ 2010-11-04 23:06  暮夏  阅读(750)  评论(0编辑  收藏  举报