检测网络状态以及程序安装状态

一、检测网络状态

1
2
3
4
5
6
7
8
9
try
{
Dns.GetHostEntry("www.google.com"); //using System.Net;
return true;
}
catch //(SocketException ex)
{
return false;
}

二、检测程序安装状态(通过注册表)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private static bool CheckSoftWartInstallState(string softWareName)
{
Microsoft.Win32.RegistryKey uninstallNode =
Microsoft.Win32.Registry.LocalMachine.OpenSubKey
(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree,
System.Security.AccessControl.RegistryRights.ReadKey);
foreach (string subKeyName in uninstallNode.GetSubKeyNames())
{
Microsoft.Win32.RegistryKey subKey = uninstallNode.OpenSubKey(subKeyName);
object displayName = subKey.GetValue("DisplayName");
if (displayName != null)
{
if (displayName.ToString().Contains(softWareName))
{
return true;
}
}
}
return false;
}

注:绿色软件无安装所以检测不到;�到;