如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !
---------------------------------

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:

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

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

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

---------------------------------------------------------------------

如果需要查看更多文章,请微信搜索公众号 csharp编程大全,需要进C#交流群群请加微信z438679770,备注进群, 我邀请你进群! ! !

C# 软件开机启动的更多相关文章

  1. Win7/8 绿色软件开机启动

    在查找番茄工作法PC端软件时,发现了淡高的文章win8绿色软件开机启动,试用了一下wintabs,的确好用! 另外,office软件中有一款 OFFICE tabs的插件,标签式的管理,非常方便快捷, ...

  2. windows10 设置软件开机启动

    在 C:\Users\your_name\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup 在这个目录下,新建你想开机启动的软 ...

  3. win10系统怎么设置软件开机启动

    win10开机自动启动软件设置教程: 1:在windows10桌面,右键点击桌面左下角的开始按钮,在弹出的菜单中选择运行菜单项. 2:这时就会打开windows10的运行窗口,在窗口中输入命令shel ...

  4. win10 设置软件开机启动项失效

    问题重现: win10系统,只要是图标右下角带盾牌标志的软件,加入系统的启动文件夹:如:C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Sta ...

  5. win10如何设置软件开机启动

    想要实现应用程序在所有的用户登录系统后都能自动启动,就把该应用程序的快捷方式放到“系统启动文件夹”里C:\ProgramData\Microsoft\Windows\Start Menu\Progra ...

  6. 设置win 10 软件开机启动

    C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp 将快捷方式粘贴进去即可

  7. Linux/CentOS 服务安装/卸载,开机启动chkconfig命令详解|如何让MySQL、Apache开机启动?

    chkconfig chkconfig在命令行操作时会经常用到.它可以方便地设置和查询不同运行级上的系统服务.这个可要好好掌握,用熟练之后,就可以轻轻松松的管理好你的启动服务了. 注:谨记chkcon ...

  8. Linux开机启动chkconfig命令详解(让MySQL、Apache开机启动)

    chkconfig chkconfig在命令行操作时会经常用到.它可以方便地设置和查询不同运行级上的系统服务.这个可要好好掌握,用熟练之后,就可以轻轻松松的管理好你的启动服务了. 注:谨记chkcon ...

  9. JavaFx 创建快捷方式及设置开机启动

    原文地址:JavaFx 创建快捷方式及设置开机启动 | Stars-One的杂货小窝 原本是想整个桌面启动器,需要在windows平台上实现开机启动,但我的软件都是jar文件,不是传统的exe文件,也 ...

随机推荐

  1. HDU - 5775-Bubble Sort(权值线段树)

    P is a permutation of the integers from 1 to N(index starting from 1). Here is the code of Bubble So ...

  2. HDU - 6570 - Wave(暴力)

    Avin is studying series. A series is called "wave" if the following conditions are satisfi ...

  3. C++ —— 输出100以内的质数

    代码如下: #include<iostream> #include<math.h> using namespace std; int main() { int i; for(i ...

  4. JVM学习第三天(JVM的执行子系统)之开篇Class类文件结构

    虽然这几天 很忙,但是学习是不能落下的,也不能推迟,因为如果推迟了一次,那么就会有无数次;加油,come on! Java跨平台的基础: 各种不同平台的虚拟机与所有平台都统一使用的程序存储格式——字节 ...

  5. Cassandra使用的各种策略

    1. 背景介绍 Cassandra 使用分布式哈希表(DHT)来确定存储某一个数据对象的节点.在 DHT 里面,负责存储的节点以及数据对象都被分配一个 token.token 只能在一定的范围内取值, ...

  6. leetcode刷题-64最小路径和

    题目 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明:每次只能向下或者向右移动一步. 示例: 输入:[  [1,3,1],  [1,5, ...

  7. js拖拽原理及简单实现(渣渣自学)

    第一步 首先简单分析下需求吧,我们就是想实现鼠标拖拽带颜色的方块时,让方块停留在鼠标松开的位置,需要计算的就是拖拽前的坐标和拖拽后的坐标,鼠标移动后相对于原位置的偏移量=目标元素的偏移量,根据这个等式 ...

  8. pthon中取整的几个方法round、int、math

    取整的几种方法:1.四舍五入 round(x) 2.向下取整  int(x) 3.取商和余 4.向上取整,需要用到math.ceil(x)(可以理解成大于x且最接近x的整数)import math 5 ...

  9. CentOS7下mysql忘记root密码的处理方法

    1.  vi /etc/my.cnf,在[mysqld]中添加 skip-grant-tables 例如: [mysqld] skip-grant-tables datadir=/var/lib/my ...

  10. [Leetcode]585. 2016年的投资(MySQL)

    题目 写一个查询语句,将 2016 年 (TIV_2016) 所有成功投资的金额加起来,保留 2 位小数. 对于一个投保人,他在 2016 年成功投资的条件是: 他在 2015 年的投保额 (TIV_ ...