First, you must request for administrative permission of the user. You can do this through your program class in your application. The code below asks the user for administrative access, then the user has the option to allow or deny it. If they deny this, this example does not launch the application.
As soon as the application is launched in administrative mode, its plain text with simple formatting. You donβt even need all the Microsoft comments included in the file, and a simple line parsing would be very good. The MSFT notes in the HOSTS file are all the necessary documentation since the HOSTS file itself is coming.
namespace Setup { using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using Setup.Forms; using System.Security.Principal; using System.Diagnostics; static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); WindowsPrincipal principal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); bool administrativeMode = principal.IsInRole(WindowsBuiltInRole.Administrator); if (!administrativeMode) { ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.Verb = "runas"; startInfo.FileName = Application.ExecutablePath; try { Process.Start(startInfo); } catch { return; } return; } Application.Run(new ShellForm()); } } }
David Anderson - DCOM
source share