| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Security.Principal;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace UAS_XmlAnalysor
- {
- static class Program
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- try
- {
- WindowsIdentity identity = WindowsIdentity.GetCurrent();
- WindowsPrincipal principal = new WindowsPrincipal(identity);
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- if (!Directory.Exists(Application.StartupPath+@"\Cache"))
- Directory.CreateDirectory(Application.StartupPath + @"\Cache");
- if (principal.IsInRole(WindowsBuiltInRole.Administrator))
- Application.Run(new Form1());
- else
- {
- //创建启动对象
- System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
- // 设置运行文件
- startInfo.FileName = Application.ExecutablePath;
- //设置启动动作,确保以管理员身份运行
- startInfo.Verb = "runas";
- //如果不是管理员,则启动UAC
- System.Diagnostics.Process.Start(startInfo);
- }
- }
- catch (Exception)
- {
- }
- }
- public static void SetAutoRun(string fileName, bool isAutoRun)
- {
- RegistryKey reg = null;
- try
- {
- if (!System.IO.File.Exists(fileName))
- throw new Exception("该文件不存在!");
- string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
- reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
- if (reg == null)
- reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
- if (isAutoRun)
- reg.SetValue(name, fileName);
- else
- reg.SetValue(name, false);
- }
- catch (Exception ex)
- {
- throw new Exception(ex.ToString());
- }
- finally
- {
- if (reg != null)
- reg.Close();
- }
- }
- }
- }
|