当前位置:网站首页>C software startup

C software startup

2021-10-14 05:07:59 zls366

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(" Software self start ");

         
            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(" apply name ", 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(" apply name ", false);
                    R_run.Close();
                    R_local.Close();
                }
            }


            // if (showinfo)
            //      MessageBox.Show(" You need administrator privileges to modify ", " Tips ");
            // Console.WriteLine(" You need administrator privileges to modify ");
            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_" + " You need administrator privileges to modify " + "\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();
            }
        }
    }
}

   After running, click the button :

 

 

  Then restart and click the button :

 

 

 

Found not ? After startup and automatic startup

Environment.CurrentDirectory There has been a change ,

In this way, if we used relative paths in our program , You can't find the corresponding file ,

How to deal with ?

Method 1:

Change the current path during program initialization

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

   Method 2: Add environment variables , Calculation line -- attribute -- Advanced system setup -- environment variable -- Set in system variable .

  static void Main(string[] args)
        {
            Console.WriteLine(" More boutique search official account :" + "dotnet Programming ");
            Console.WriteLine("C# Technology exchange group :"+" Add small make up WeChat " + "zls20210502");
            Console.WriteLine(" Keep your notes in mind : Add group ");
            Console.ReadKey();
        }

  

版权声明
本文为[zls366]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/10/20211002145410633a.html

随机推荐