C#程序 实现如下功能: 1. 读取注册表SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\下shell的值保存为字符串strShell,strShell的字符串格式举例为"C:\2.4.7460\autostart.exe doctorold.exe"; 2. 将strShell识别字符串中路径(例如C:\2.4.7460\)并保存为strPath; 3. 读取当前目录下的DoctorOld.exe文件版本只保留前三段版本号(例:2.4.7460.12546保留2.4.7460)保存为字符串strNewVer; 4. 读取strPath目录下的DoctorOld.exe文件版本只保留前三段版本号(例:2.4.7460.12546保留2.4.7460)保存为字符串strOldVer; 5. 新建"C:\"+strNewVer文件夹,其中包括判断是否存在,进行提示“版本相同是否进行强制更新”,如果强制更新则将旧文件夹进行重命名,重命名规则为在文件夹后加当前时间,将新文件夹路径给strPath赋值; 6. 复制当前目录内所有文件和文件夹至"C:\"+strNewVer; 7. 复制strPath目录下的1.txt、2.txt、3.txt文件至"C:\"+strNewVer直接覆盖; 8. 查找strShell中是否含有“Doctorold.exe”、"Nurse.exe"、"USVoltDlg.exe"、"Water.exe",分别存布尔型变量; 9. 如果含有“Doctorold.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe doctorold.exe"; 10. 如果含有“Nurse.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe Nurse.exe"; 11. 如果含有“Water.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe Water.exe"; 12. 如果含有“USVoltDlg.exe”则修改注册表HKEY_LOCAL_MACHINE\System\Setup\下shell的值为"C:\"+strNewVer+"autostart.exe USVoltDlg.exe"; 13. 打开“C:\”路径下名为“UpdaterLog.txt”文本文档,在文件最后行添加:当前日期+时间+“:”+strOldVer+"-->"+strNewVer;
时间: 2023-07-05 07:06:18 浏览: 121
以下是实现该功能的C#程序代码:
```c#
using System;
using System.IO;
using System.Linq;
using Microsoft.Win32;
class Program
{
static void Main(string[] args)
{
// 读取注册表SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\下shell的值
string strShell = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "Shell", "").ToString();
// 从shell字符串中提取路径
string strPath = strShell.Split(' ').FirstOrDefault(s => s.EndsWith("\\"));
// 读取当前目录下的DoctorOld.exe文件版本
string strNewVer = FileVersionInfo.GetVersionInfo("DoctorOld.exe").FileVersion;
strNewVer = string.Join(".", strNewVer.Split('.').Take(3));
// 读取strPath目录下的DoctorOld.exe文件版本
string strOldVer = FileVersionInfo.GetVersionInfo(Path.Combine(strPath, "DoctorOld.exe")).FileVersion;
strOldVer = string.Join(".", strOldVer.Split('.').Take(3));
// 新建版本文件夹并复制文件
string strNewPath = Path.Combine("C:\\", strNewVer);
if (Directory.Exists(strNewPath))
{
Console.Write("版本相同是否进行强制更新(Y/N):");
if (Console.ReadKey().Key == ConsoleKey.Y)
{
string strOldPath = strNewPath + "_" + DateTime.Now.ToString("yyyyMMddHHmmss");
Directory.Move(strNewPath, strOldPath);
}
else
{
return;
}
}
Directory.CreateDirectory(strNewPath);
foreach (string file in Directory.GetFiles(".", "*", SearchOption.AllDirectories))
{
File.Copy(file, Path.Combine(strNewPath, Path.GetFileName(file)), true);
}
// 复制txt文件和修改注册表
foreach (string file in new[] { "1.txt", "2.txt", "3.txt" })
{
File.Copy(Path.Combine(strPath, file), Path.Combine(strNewPath, file), true);
}
bool hasDoctorOld = strShell.Contains("Doctorold.exe");
bool hasNurse = strShell.Contains("Nurse.exe");
bool hasWater = strShell.Contains("Water.exe");
bool hasUSVoltDlg = strShell.Contains("USVoltDlg.exe");
if (hasDoctorOld)
{
Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\Setup", "Shell", "\"" + strNewPath + "\\autostart.exe doctorold.exe\"");
}
if (hasNurse)
{
Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\Setup", "Shell", "\"" + strNewPath + "\\autostart.exe Nurse.exe\"");
}
if (hasWater)
{
Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\Setup", "Shell", "\"" + strNewPath + "\\autostart.exe Water.exe\"");
}
if (hasUSVoltDlg)
{
Registry.SetValue(@"HKEY_LOCAL_MACHINE\System\Setup", "Shell", "\"" + strNewPath + "\\autostart.exe USVoltDlg.exe\"");
}
// 写入日志
using (var writer = new StreamWriter(@"C:\UpdaterLog.txt", true))
{
writer.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ": " + strOldVer + " --> " + strNewVer);
}
}
}
```
阅读全文