using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Win32;
using System.Windows.Forms;
namespace UpdateFile
{
//注册表RegistryKey类在.NET的Microsoft.Win32命名空间中
/// <summary>
/// 用于更新管理软件文件使用
/// 作者:WJS
/// 时间:2013-12-24
/// </summary>
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
CopyFileToInstallLocation();
}
/// <summary>
/// 复制文件到安装目录
/// </summary>
private void CopyFileToInstallLocation()
{
string InstallLocation = FindInstallationDirectory();
if (InstallLocation==null)
{
labelControl.Text = "您未安装回弹仪数据处理软件!";
}
else
{//复制文件到安装目录
//获取需要复制文件的路径
string FileDir = System.IO.Directory.GetCurrentDirectory();
FileDir += "\\Data\\";
CopyFileToInstallDir(FileDir, InstallLocation);
labelControl.Text = "更新完成!";
}
}
/// <summary>
/// 查找安装路径
/// </summary>
/// <returns></returns>
private string FindInstallationDirectory()
{//{F527CD43-4692-43FF-8716-172D4631F905}
//InstallLocation 安装路径
string MykeyName = "{F527CD43-4692-43FF-8716-172D4631F905}";
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE", false);
RegistryKey microsoft = software.OpenSubKey("Microsoft", false);
RegistryKey windows = microsoft.OpenSubKey("Windows", false);
RegistryKey currentVersion = windows.OpenSubKey("CurrentVersion", false);
RegistryKey uninstall = currentVersion.OpenSubKey("Uninstall", false);
string[] subkeyNames = uninstall.GetSubKeyNames();
foreach (string keyName in subkeyNames)
{
if (keyName == MykeyName)
{
RegistryKey Name = uninstall.OpenSubKey(MykeyName, false);
string InstallLocation = (string)Name.GetValue("InstallLocation");
return InstallLocation;
}
}
return null;
}
private void CopyFileToInstallDir( string FileDir,string InstallLocation)
{
//查找目标文件夹下所有文件和文件夹
DirectoryInfo dires = new DirectoryInfo(FileDir);
FileInfo[] files = dires.GetFiles();
foreach (FileInfo f in files)
{
if (!Directory.Exists(InstallLocation))
Directory.CreateDirectory(InstallLocation);
File.Copy(f.FullName, InstallLocation+f.Name,true);
}
DirectoryInfo[] dir = dires.GetDirectories();
foreach (DirectoryInfo d in dir)
{
CopyFileToInstallDir(d.FullName,InstallLocation+d.Name+"\\");
}
}
}
}
c# 注册表操作,用于更新软件
最新推荐文章于 2024-07-02 11:52:39 发布