ManagerUtil.cs 4.1 KB
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StroreManager
{
    public class ManagerUtil
    {
        public static void AutoRun(string strName, bool value)
        {
            try
            {
                //创建启动对象 
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                //设置运行文件
                startInfo.FileName = System.Windows.Forms.Application.StartupPath + "\\AuToRunManager.exe";
                //设置启动参数 
                startInfo.Arguments = String.Join(" ", new object[2] { strName, value });
                //设置启动动作,确保以管理员身份运行
                startInfo.Verb = "runas";
                //如果不是管理员,则启动UAC 
                System.Diagnostics.Process.Start(startInfo);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        //public static void AutoRun(string filePath, bool isAuto)
        //{
        //    try
        //    {
        //        string strnewName = filePath.Substring(filePath.LastIndexOf("\\") + 1);
        //        if (isAuto)
        //        {
        //            RegistryKey Local = Registry.LocalMachine;
        //            RegistryKey runKey = Local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\");
        //            runKey.SetValue(strnewName, filePath);
        //            Local.Close();
        //            MessageBox.Show("程序" + strnewName + "开机启动设置完成,重新启动计算机后即可生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        }
        //        else
        //        {
        //            //修改注册表,使程序开机时不自动执行。             
        //            Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
        //            Rkey.DeleteValue(strnewName, false);
        //            Rkey.Close();
        //            MessageBox.Show("程序" + strnewName + "开机启动已取消!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.ToString());
        //    }
        //}

        private static string fileName = "auToData.data";
        private static object LockData="";
        internal static void SaveAutoRunToFile(List<StoreExeBean> exeList)
        {
            lock (LockData)
            {
                string path = Application.StartupPath;
                string FilePath = path + @"\" + fileName;
                if (!File.Exists(FilePath))
                {
                    File.Create(FilePath);
                }

                string str = "";
                foreach (StoreExeBean exe in exeList)
                {
                    if (exe.IsAutoRun)
                    {
                        str = str + exe.AuToFileData()+"\r\n";
                    }
                }

                File.WriteAllText(FilePath, str);
            }
        }

        internal static List<StoreExeBean> GetAuToExeList()
        {
            lock (LockData)
            {
                string path = Application.StartupPath;
                string FilePath = path + @"\" + fileName;
                List<StoreExeBean> result = new List<StoreExeBean>();
                if (File.Exists(FilePath))
                {
                    string[] fileArray = File.ReadAllLines(FilePath, Encoding.UTF8);
                    foreach (string str in fileArray)
                    {
                        StoreExeBean store = StoreExeBean.ConverToBean(str);
                        if (store != null)
                        {
                            result.Add(store);
                        }
                    }
                }
                return result;
            }
        }
    }
}