C#winform检测电脑安装的.netframework版本和是否安装了某软件

代码如下:

        //C#获取已安装 .NET Framework 版本
private static string[] GetDotNetVersions()
{
DirectoryInfo[] directories = new DirectoryInfo(
Environment.SystemDirectory + @"\..\Microsoft.NET\Framework").GetDirectories("v?.?.*");
ArrayList list = new ArrayList();
foreach (DirectoryInfo info2 in directories)
{
list.Add(info2.Name.Substring(1));
}
return (list.ToArray(typeof(string)) as string[]);
} /// <summary>
/// 确认电脑上是否安装有某个程序
/// </summary>
/// <param name="softWareName">程序安装后的名称</param>
/// <returns>true: 有安裝, false:沒有安裝</returns>
private static bool CheckSoftWartInstallState(string softWareName = "Adobe Reader")
{
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;
// MessageBox.Show(displayName.ToString());
}
}
}
return false;
} /// <summary>
/// 程序运行前检测
/// </summary>
public static void ApplicationBeforeRunCheck()
{
string[] deviceName = { "PL-2303 USB-to-Serial",
"Microsoft ReportViewer 2010 Redistributable",
"二代证读写机具USB驱动"};
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
string[] netList = GetDotNetVersions();
bool dotNetIsOk = false;
for (int i = 0; i < netList.Length; i++)
{
if (netList[i][0] == 4)
dotNetIsOk = true;
}
if (!dotNetIsOk)
{
Process.Start(Path.Combine(baseDirectory, "4.exe"));
while (true)
{
for (int i = 0; i < netList.Length; i++)
if (netList[i][0] == 4) break;
Thread.Sleep(1000);
}
}
for (int i = 0; i < deviceName.Length; i++)
{
if (!CheckSoftWartInstallState(deviceName[i]))
{
Process.Start(Path.Combine(baseDirectory, ((i + 1) + ".exe")));
while (true)
{
if (CheckSoftWartInstallState(deviceName[i]))
break;
Thread.Sleep(1000);
}
}
}
//软件安装完成
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue("jkytj", baseDirectory);
rk2.Close();
rk.Close();
//软件自启完成
}
上一篇:mahout源码分析之DistributedLanczosSolver(五)Job over


下一篇:Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系统)