1

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;
    }
1
  • Only way is disabling UAC and I doubt anyone would want that... Commented Oct 27, 2016 at 9:02

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I aware of that but is there any way to provide such rights programmatically / bypass / handle such alert without manual intervention..that's what I needed, please suggest me if you got any idea
The whole point of the UAC popup is to prevent un-authorised changes by applications. If there was a way to bypass that, there would be nothing to stop any application from changing any setting whatsoever.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.