C# 软件开机启动

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();
            }
        }
    }
}

运行后点击按钮:

16d3f96b98743be5ae103e2d94672420.png

,然后重启后点击按钮:

e6f40324bc2e9c1e898d467190f4899d.png

发现没?开机自动启动后

Environment.CurrentDirectory 发生了变更,

这样在在我们程序中原本如果使用相对路径进行处理的,就找不见相应的文件了,

怎么处理?

方法1:

在程序初始化的时候改变下当前路径

public Form1()
        {
            InitializeComponent();
           Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
        }

方法2:添加环境变量 , 计算行--属性--高级系统设置--环境变量--系统变量里面设置.

——————————

经典回顾

      因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“赞”和“在看”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点击“赞”和“在看”支持我们吧!

往期推荐

C# WPF框架Caliburn.Micro入门实例1

C# WPF MVVM项目实战(进阶①)

C# WPF MVVM项目实战(进阶②)

C# WPF框架Caliburn.Micro快速搭建

C# WPF项目实战

C# WPF mvvm模式下combobox绑定(list<enum>、Dictionary<int,string>)

C# WPF MVVM模式下在主窗体显示子窗体并获取结果

C# WPF Caliburn.Micro框架下利用Mef加载其它项目界面

C# WPF文本框TextEdit不以科学计数法显示

C# 通过正则表达式来限制控件输入有效性

C# datagridview、datagrid、GridControl增加行号

C# =>符号的使用

C# 无意间写了一段线程死锁的代码

C# 看懂这100+行代码,你就真正入门了(经典)

C# WPF项目实战(经典)

WPF 如何修改button圆角(经典)

WPF XAML 为项目设置全局样式

f1a160a3cd01c3b35b3f54a05284dcac.png

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zls365365

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值