网吧延迟启动工具以及源代码
缘由 好久没给网吧维护了,最近给日本网吧维护的时候, 由于要在工作站开机启动之后执行一些初始化程序,要用到一个延迟启动程序。(程序是前辈写的,帮了很多忙。鞠躬) 但是在日本语系统下无法正常运行 报错如下: 如果要解决该错误,需要在区域选项中,将程式区域改为中国即可。如此一来会产生一个新的问题, 某些程式会用区域来判断用户的location, 会导致一些软件无法正常运行。 于是我依葫芦画瓢,手搓了一个能在日本语系统下正常运行的延迟启动工具 功能和用法 简介 该延迟启动工具的本体只有一个exe执行文件和ini配置文件 执行文件会读取ini文件中的配置,然后执行相关任务 配置文件说明 配置文件包含TargetPath,FileExtensions,Timeout选项 TargetPath: 所需要执行的目录路径(绝对路径) FileExtenions: 需要执行的文件格式 Timeout: 延迟时间 毫秒为单位 用法 在给工作站开超级时,将Hiderun.exe 添加到系统启动项 根据你自身需求,在配置文件中“config.ini”填写相对应的路径和文件格式,以及时间。 日志 程序会在程序根目录生成log文件记录执行状态和报错,遇到问题可以在日志中查看明细。 程序执行演示 程序下载 解压密码: www.itiohub.com HideRun.zip 源代码 using System; using System.IO; using System.Diagnostics; using System.Threading; using System.Runtime.ExceptionServices; class Program { static string logFilePath; static void Main() { // 在程序根目录生成日志文件 string logFilePath = GenerateLogFilePath(); CreateLogFile(logFilePath); // 隐藏自身窗口 IntPtr handle = Process.GetCurrentProcess().MainWindowHandle; ShowWindow(handle, SW_HIDE); LogToFile(logFilePath, "延迟启动工具 By 小四 www.itiohub.com"); // 读取配置文件 var configFile = "config.ini"; var config = ReadConfig(configFile); if (config != null) { // 获取配置信息 string targetPath = config["TargetPath"]; string[] fileExtensions = config["FileExtensions"].Split(','); int timeout = int.Parse(config["Timeout"]); // 后台运行 RunInBackground(logFilePath, targetPath, fileExtensions, timeout); } else { LogToFile(logFilePath, "配置文件读取失败。 请检查配置文件config.ini是否存在"); } Thread.Sleep(5000); } //创建日志文件 static void CreateLogFile(string logFilePath) { try { using (StreamWriter sw = File.CreateText(logFilePath)) { sw.WriteLine("程序启动时间: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } } catch (Exception ex) { Console.WriteLine($"创建日志文件时发生错误: {ex.Message}"); } } //后台运行 static void RunInBackground(string logFilePath, string targetPath, string[] fileExtensions, int timeout) { try { // 在目标路径中查找并运行匹配的文件 foreach (var fileExtension in fileExtensions) { var files = Directory.GetFiles(targetPath, $"*.{fileExtension}"); foreach (var file in files) { LogToFile(logFilePath, $"运行文件: {file}"); var processStartInfo = new ProcessStartInfo(file); processStartInfo.WindowStyle = ProcessWindowStyle.Hidden; var process = Process.Start(processStartInfo); if (process != null) { // 等待超时时间 if (!process.WaitForExit(timeout)) { LogToFile(logFilePath, $"文件 {file} 运行超时。"); } // 可以添加其他处理逻辑,例如记录日志等 } else { LogToFile(logFilePath, $"无法启动文件 {file}。"); } } } // 执行完所有文件后,延迟5秒并退出 LogToFile(logFilePath, "所有文件已执行,程序将在5秒后退出。"); Thread.Sleep(5000); } catch (Exception ex) { LogToFile(logFilePath, $"发生错误: {ex.Message}"); } } //读取配置文件 static IniConfig ReadConfig(string filePath) { try { var config = new IniConfig(); var lines = File.ReadAllLines(filePath); foreach (var line in lines) { var parts = line.Split('='); if (parts.Length == 2) { config.Add(parts[0].Trim(), parts[1].Trim()); } } return config; } catch (Exception ex) { LogToFile(logFilePath, $"读取配置文件时发生错误: {ex.Message}"); return null; } } //日志文件 static void LogToFile(string logFilePath, string message) { try { using (StreamWriter sw = File.AppendText(logFilePath)) { string logMessage = $"{DateTime.Now.ToString("yyyy_MM_dd_HH_mm")}: {message}"; Console.WriteLine(logMessage); sw.WriteLine(logMessage); } } catch (Exception ex) { Console.WriteLine($"写入日志文件时发生错误: {ex.Message}"); } } //日志文件名称generate static string GenerateLogFilePath() { string fileName = $"hiderun{DateTime.Now.ToString("yyyy_MM_dd_HH_mm")}.log"; return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); } [System.Runtime.InteropServices.DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); private const int SW_HIDE = 0; } //ini配置文件 ky value class IniConfig { private readonly System.Collections.Generic.Dictionary<string, string> config; public IniConfig() { config = new System.Collections.Generic.Dictionary<string, string>(); } public string this[string key] { get { return config.ContainsKey(key) ? config[key] : null; } } public void Add(string key, string value) { if (!config.ContainsKey(key)) { config.Add(key, value); } } }