C# 软件开机启动
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Microsoft.Win32; using System.IO; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); // Class1.test(); // Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; } private void button1_Click( object sender, EventArgs e) { MessageBox.Show( "软件自启动" ); String LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log" ); MessageBox.Show(Environment.CurrentDirectory+ "\n" +LogPath); //Class1.test(); } private void Form1_Load( object sender, EventArgs e) { AutoStart(); } private void AutoStart( bool isAuto = true , bool showinfo = true ) { try { if (isAuto == true ) { RegistryKey R_local = Registry.CurrentUser; //RegistryKey R_local = Registry.CurrentUser; RegistryKey R_run = R_local.CreateSubKey( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" ); R_run.SetValue( "应用名称" , Application.ExecutablePath); R_run.Close(); R_local.Close(); } else { RegistryKey R_local = Registry.CurrentUser; //RegistryKey R_local = Registry.CurrentUser; RegistryKey R_run = R_local.CreateSubKey( @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run" ); R_run.DeleteValue( "应用名称" , false ); R_run.Close(); R_local.Close(); } } // if (showinfo) // MessageBox.Show("您需要管理员权限修改", "提示"); // Console.WriteLine("您需要管理员权限修改"); catch (Exception ex) { String LogPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log" ); if (!Directory.Exists(LogPath)) Directory.CreateDirectory(LogPath); if (!File.Exists(LogPath + "\\log.txt" )) File.Create(LogPath + "\\log.txt" ).Close(); string fileName = LogPath + "\\log.txt" ; string content = DateTime.Now.ToLocalTime() + " 0001_" + "您需要管理员权限修改" + "\n" + ex.StackTrace + "\r\n" ; Logger(fileName, content); } } public static void disPlay( string path) { MessageBox.Show(path); } public void Logger( string fileName, string content) { using (StreamWriter sw = new StreamWriter(fileName, true )) { sw.Write(content); sw.Close(); sw.Dispose(); } } } } |
运行后点击按钮:
然后重启后点击按钮:
发现没?开机自动启动后
Environment.CurrentDirectory 发生了变更,
这样在在我们程序中原本如果使用相对路径进行处理的,就找不见相应的文件了,
怎么处理?
方法1:
在程序初始化的时候改变下当前路径
1 2 3 4 5 | public Form1() { InitializeComponent(); Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; } |
方法2:添加环境变量 , 计算行--属性--高级系统设置--环境变量--系统变量里面设置.
1 2 3 4 5 6 7 | static void Main( string [] args) { Console.WriteLine( "更多精品搜索公众号:" + "dotnet编程大全" ); Console.WriteLine( "C#技术交流群:" + "添加小编微信" + "zls20210502" ); Console.WriteLine( "切记备注:加群" ); Console.ReadKey(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异