Munindar Reddy
Munindar Reddy

Reputation: 9

how to modify hosts file using c# with an admin rights and without manual intervention

I've been trying with number of ways but I'm unable to avoid the alert which says 'Do you want to open the application as administrator'. can some one suggest such piece of code which avoids/handles the alert to add new entry into hosts file. Thanks in advance..

public bool ModifyHostsFile(string sEntryIPAddr, string sEntryURL)
    {
        try
        {
            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;
                //Process.Start(startInfo);
                //bool bStatus = GrantAccess(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts"));
                using (StreamWriter w = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), @"drivers\etc\hosts")))
                {
                    w.WriteLine(sEntryIPAddr + " " + sEntryURL);
                }
                Application.Exit();
            }
            return true;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return false;
        }   
    }

    private bool GrantAccess(string fullPath)
    {
        DirectoryInfo dInfo = new DirectoryInfo(fullPath);
        DirectorySecurity dSecurity = dInfo.GetAccessControl();
        dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
        dInfo.SetAccessControl(dSecurity);
        return true;
    }

Upvotes: 1

Views: 1510

Answers (2)

Dutchman
Dutchman

Reputation: 183

I don't have the rep to comment to ask some question I have but instead I will answer based on assumptions.

Your goal

To run an application with administrative rights

Why the UAC popup?

The popup you get asking for Admin Rights is to prevent applications from simply taking admin rights without your knowledge and thereby modifying your system's critical files

How to prevent it?

I HIGHLY recommend not taking this step but the first option that comes to mind is to disable UAC (User Account Controls) from your control panel

Alternative

An alternative could be to run the program as a scheduled task, set to run with the highest privileges. Then you can execute the program by running the scheduled task and have admin access without the UAC popup. This can be done via command-line or via the GUI and still requires admin privileges for the creation of the schedules task

Upvotes: 0

navigator
navigator

Reputation: 1708

No, you definitely need admin access to modify the file - Or else any virus could hijack the hosts file and redirect all browsers requests to a malicious site.

Upvotes: 2

Related Questions