前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C# 软件开机启动

C# 软件开机启动

作者头像
zls365
发布2020-08-19 11:27:54
6180
发布2020-08-19 11:27:54
举报
文章被收录于专栏:CSharp编程大全
代码语言:javascript
复制
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:

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

代码语言:javascript
复制
 public Form1()
        {
            InitializeComponent();
           Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
        }

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 CSharp编程大全 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档