| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- using System;
- using System.IO;
- using System.Security.Principal;
- using System.Windows.Forms;
- namespace NotePad
- {
- static class Program
- {
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- try
- {
- WindowsIdentity identity = WindowsIdentity.GetCurrent();
- WindowsPrincipal principal = new WindowsPrincipal(identity);
- if (!Directory.Exists("Note"))
- Directory.CreateDirectory("Note");
- if (principal.IsInRole(WindowsBuiltInRole.Administrator))
- Application.Run(new NotePadForm());
- else
- {
- //创建启动对象
- System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
- // 设置运行文件
- startInfo.FileName = Application.ExecutablePath;
- //设置启动动作,确保以管理员身份运行
- startInfo.Verb = "runas";
- //如果不是管理员,则启动UAC
- System.Diagnostics.Process.Start(startInfo);
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- }
- }
|