C#程序 实现如下功能: 1. 读取注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值保存为字符串strShell,strShell的字符串格式举例为"C:\2.4.7460.12546\autostart.exe doctorold.exe"; 2. 将strShell识别字符串中路径的子字符串保存为strPath; 3. 读取当前目录下的DoctorOld.exe文件版本只保留前三段版本号(例:2.4.7460.12546保留2.4.7460)保存为字符串strNewVer; 3. 读取strPath目录下的DoctorOld.exe文件版本只保留前三段版本号(例:2.4.7460.12546保留2.4.7460)保存为字符串strOldVer; 4. 新建"C:\"+strNewVer文件夹,其中包括判断是否存在,进行提示“版本相同是否进行强制更新”,如果强制更新则将旧文件夹进行重命名,重命名规则为在文件夹后加当前时间,将新文件夹路径给strPath赋值; 5. 复制当前目录内所有文件和文件夹至"C:\"+strNewVer; 6. 复制strPath目录下的1.txt、2.txt、3.txt文件至"C:\"+strNewVer直接覆盖; 7. 查找strShell中是否含有“Doctorold.exe”、"Q.exe"、"W.exe",分别存布尔型变量; 8. 如果含有“Doctorold.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe doctorold.exe"; 9. 如果含有“Q.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe Q.exe"; 10. 如果含有“W.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe W.exe"; 11. 读取C:\UpdaterLog.txt,在文件最后行添加:当前日期+时间+“:”+strOldVer+"-->"+strNewVer;
时间: 2023-07-07 19:05:28 浏览: 77
以下是 C# 程序实现该功能的示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.Win32;
namespace Updater
{
class Program
{
static void Main(string[] args)
{
// 读取注册表中的 shell 值
string strShell = Registry.GetValue(@"HKEY_LOCAL_MACHINE\System\Setup", "shell", "") as string;
if (string.IsNullOrEmpty(strShell))
{
Console.WriteLine("未能读取到注册表中的 shell 值!");
return;
}
// 提取路径子串
string pattern = @"([A-Za-z]:\\[^\\ ]+)";
MatchCollection matches = Regex.Matches(strShell, pattern);
string strPath = matches.Count > 0 ? matches[0].Value : "";
if (string.IsNullOrEmpty(strPath))
{
Console.WriteLine("未能从 shell 值中提取到路径!");
return;
}
// 读取当前目录下 DoctorOld.exe 的版本号
string strNewVer = GetFileVersion("DoctorOld.exe");
// 读取 strPath 目录下 DoctorOld.exe 的版本号
string strOldVer = GetFileVersion(Path.Combine(strPath, "DoctorOld.exe"));
// 创建新文件夹
string newFolder = Path.Combine("C:\\", strNewVer);
bool forceUpdate = false;
if (Directory.Exists(newFolder))
{
Console.Write("已存在版本为 {0} 的文件夹,是否强制更新?(Y/N)", strNewVer);
forceUpdate = Console.ReadLine().Trim().ToLower() == "y";
if (forceUpdate)
{
string oldFolder = newFolder + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
Directory.Move(newFolder, oldFolder);
Console.WriteLine("已将旧文件夹重命名为:{0}", oldFolder);
}
else
{
Console.WriteLine("未进行强制更新,程序将退出。");
return;
}
}
Directory.CreateDirectory(newFolder);
Console.WriteLine("已创建新文件夹:{0}", newFolder);
// 复制文件和文件夹
CopyAllFilesAndFolders(Environment.CurrentDirectory, newFolder);
// 复制指定文件
string[] filesToCopy = { "1.txt", "2.txt", "3.txt" };
foreach (string file in filesToCopy)
{
string srcFile = Path.Combine(strPath, file);
string destFile = Path.Combine(newFolder, file);
if (File.Exists(srcFile))
{
File.Copy(srcFile, destFile, true);
Console.WriteLine("已复制文件:{0}", file);
}
}
// 修改 shell 值
bool hasDoctorOld = strShell.Contains("Doctorold.exe");
bool hasQ = strShell.Contains("Q.exe");
bool hasW = strShell.Contains("W.exe");
if (hasDoctorOld)
{
string newShell = string.Format("\"{0}\\autostart.exe\" doctorold.exe", newFolder);
Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\Setup", "shell", newShell);
Console.WriteLine("已修改 shell 值为:{0}", newShell);
}
if (hasQ)
{
string newShell = string.Format("\"{0}\\autostart.exe\" Q.exe", newFolder);
Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\Setup", "shell", newShell);
Console.WriteLine("已修改 shell 值为:{0}", newShell);
}
if (hasW)
{
string newShell = string.Format("\"{0}\\autostart.exe\" W.exe", newFolder);
Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\Setup", "shell", newShell);
Console.WriteLine("已修改 shell 值为:{0}", newShell);
}
// 记录更新日志
string logFile = "C:\\UpdaterLog.txt";
string logLine = string.Format("{0}:{1}-->{2}", DateTime.Now.ToString(), strOldVer, strNewVer);
File.AppendAllLines(logFile, new[] { logLine });
Console.WriteLine("已记录更新日志:{0}", logLine);
}
static string GetFileVersion(string filePath)
{
if (!File.Exists(filePath))
{
return "";
}
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(filePath);
string version = fvi.FileVersion;
string[] parts = version.Split('.');
if (parts.Length >= 3)
{
version = string.Join(".", parts.Take(3));
}
return version;
}
static void CopyAllFilesAndFolders(string sourceFolder, string destFolder)
{
DirectoryInfo dir = new DirectoryInfo(sourceFolder);
if (!dir.Exists)
{
return;
}
foreach (FileInfo file in dir.GetFiles())
{
string destFile = Path.Combine(destFolder, file.Name);
file.CopyTo(destFile, true);
Console.WriteLine("已复制文件:{0}", file.Name);
}
foreach (DirectoryInfo subdir in dir.GetDirectories())
{
string destSubdir = Path.Combine(destFolder, subdir.Name);
CopyAllFilesAndFolders(subdir.FullName, destSubdir);
}
}
}
}
```
注意事项:
- 运行程序需要管理员权限,否则可能无法修改注册表。
- 由于操作涉及文件复制和重命名等较危险的操作,请谨慎使用该程序。建议在测试环境下进行测试。