using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net.Http; using HtmlAgilityPack; using System.Linq; using System.Diagnostics; class Update { public void Update1(string args) { string[] str = args.Split(','); string iisServerBaseUrl = "http://192.168.202.23:8081/"; // IIS服务器基本URL Console.WriteLine("args[0]>>"+ str[0]); Console.WriteLine("args[1]>>" + str[1]); string iisFolder = ""; // IIS服务器上的文件夹路径 string localFolderBase = AppDomain.CurrentDomain.BaseDirectory; // 本地文件夹的基本路径 SynchronizeFiles(iisServerBaseUrl, iisFolder, localFolderBase); string fileName = "vsion.txt"; // 指定要操作的文件名 string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); // 构建完整的文件路径 // 读取文本文件内容 string fileContent = File.ReadAllText(filePath); // 在文件内容中进行修改 fileContent = str[0];//args; // 将 "old text" 替换为 "new text" // 将修改后的内容写回文件 File.WriteAllText(filePath, fileContent); Console.WriteLine("文件已修改并保存。"); // 继续运行应用程序的主要逻辑 Console.WriteLine("应用程序启动...!!!"+ args); // 设置要启动的应用程序名称和参数 string appName = str[1];//"XGLFinishPro.exe"; // 替换为你的目标控制台应用程序的名称 string appArguments = " ";// 替换为你的应用程序需要的参数 // 创建进程启动信息 ProcessStartInfo startInfo = new ProcessStartInfo { FileName = appName, Arguments = appArguments, RedirectStandardOutput = false, // 可以选择是否重定向标准输出 UseShellExecute = false, // 必须设置为false,以便在控制台中启动应用程序 CreateNoWindow = false // 设置为true,以便在后台启动应用程序 }; // 创建并启动进程 Process process = new Process { StartInfo = startInfo }; process.Start(); Environment.Exit(0); // 在这里添加你的应用程序的主要逻辑 Console.ReadLine(); } public void SynchronizeFiles(string serverBaseUrl, string serverFolder, string localFolder) { System.Threading.Thread.Sleep(3000); using (HttpClient client = new HttpClient()) { try { HttpResponseMessage response = client.GetAsync(new Uri(serverBaseUrl + serverFolder)).Result; response.EnsureSuccessStatusCode(); string responseContent = response.Content.ReadAsStringAsync().Result; // 使用HtmlAgilityPack解析HTML内容 HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(responseContent); // 查找所有包含文件名和文件夹名的标签 var links = doc.DocumentNode.SelectNodes("//a"); if (links != null) { foreach (var link in links) { string itemName = link.InnerText; string itemUrl = serverBaseUrl + serverFolder + itemName; string localPath = Path.Combine(localFolder, itemName); if (link.InnerText.StartsWith("")) { // 这是一个文件夹 if (!Directory.Exists(localPath)) { Directory.CreateDirectory(localPath); } // 递归处理文件夹中的内容 SynchronizeFiles(serverBaseUrl, serverFolder + itemName + "/", localPath); } else { // 这是一个文件 if (!string.Equals(itemName, "web.config", StringComparison.OrdinalIgnoreCase)) { // 检查文件类型是否需要更新 string fileExtension = Path.GetExtension(itemName).ToLower(); if (ShouldUpdateFile(fileExtension)) { if (File.Exists(localPath)) { DownloadFile(itemUrl, localPath); Console.WriteLine($"已下载并覆盖文件: {itemName}"); } else { DownloadFile(itemUrl, localPath); Console.WriteLine($"已下载文件: {itemName}"); } } else { Console.WriteLine($"不需要更新的文件类型: {fileExtension}"); } } } } } else { Console.WriteLine("未找到文件列表"); } } catch (HttpRequestException ex) { Console.WriteLine($"HTTP请求失败: {ex.Message}"); } catch (Exception ex) { Console.WriteLine($"发生错误: {ex.Message}"); } } } public bool ShouldUpdateFile(string fileExtension) { // 添加需要更新的文件类型 string[] allowedExtensions = { ".exe", ".dll", ".json", ".xml" }; // 如果文件类型在允许更新的文件类型列表中,则返回 true,否则返回 false return allowedExtensions.Contains(fileExtension); } public void DownloadFile(string fileUrl, string localFilePath) { using (HttpClient client = new HttpClient()) { try { HttpResponseMessage response = client.GetAsync(new Uri(fileUrl)).Result; response.EnsureSuccessStatusCode(); using (Stream contentStream = response.Content.ReadAsStreamAsync().Result) { using (Stream fileStream = File.Create(localFilePath)) { contentStream.CopyTo(fileStream); } } Console.WriteLine($"已成功下载文件: {localFilePath}"); } catch (HttpRequestException ex) { // 处理连接断开或其他HTTP请求错误 Console.WriteLine($"HTTP请求失败: {ex.Message}"); // 在这里可以添加重试逻辑或其他处理方式 } catch (Exception ex) { Console.WriteLine($"发生错误: {ex.Message}"); } } } public bool AreFilesEqual(string file1Url, string file2Path) { byte[] file1Content = File.ReadAllBytes(file2Path); using (HttpClient client = new HttpClient()) { byte[] file2Content = client.GetByteArrayAsync(file1Url).Result; return StructuralComparisons.StructuralEqualityComparer.Equals(file1Content, file2Content); } } }