Unable to access and block url with Host file

Elanchezhiyan P 100 Reputation points

Hi Team,

I have created a Windows form application, which needs to block the URL with the host file. The host file is located at C:\Windows\System32\drivers\etc\hosts. This application will create a backup file (.bak) and make changes to block a few URLs for test purposes. When I try to access the location, I get the access denied error. When I gave admin rights to the form, still it throws an error. Attached video link and git URL (source code) for your reference. Please let me know what changes need to be updated. Git URL: https://github.com/Elanchezhiyan-AIT/BlockUrlViaForms
Video link: https://ttprivatenew.s3.amazonaws.com/pulse/elanchezhiyan_gmail/attachments/21310239/TinyTake17-04-2023-06-18-50.mp4 Host file image for blocking URLs:User's image

Also, please confirm if the above format is correct for blocking the URLs in the host file.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,857 questions
Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
10,810 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,385 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,421 questions
0 comments No comments
{count} votes

Accepted answer
  1. Limitless Technology 44,001 Reputation points

    Hello there, This seems like a standard permissions issue. The account running the application does not have the correct permissions to access the folder. If you are debugging from Visual Studio, then try running Visual Studio as an administrator. If you are running an installation of the application then run the application itself as an administrator. Hope this resolves your Query !! --If the reply is helpful, please Upvote and Accept it as an answer--

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,891 Reputation points

    the host file is typically read only. you need to change the permission to write before save. you may need to do the same to the etc folder.

  2. RLWA32 40,951 Reputation points

    If the installed antivirus software prevents changes to the hosts file while the system is running you may be able to achieve your objective by using the Windows API MoveFileExW function to replace the file upon a system restart by using the MOVEFILE_DELAY_UNTIL_REBOOT flag. For example, the following console application sets the necessary values in the registry to save the existing hosts file to hosts.bak and rename the hosts.new file that contains changes when the system is restarted.

        class Program
            const int MOVEFILE_REPLACE_EXISTING = 0x00000001;
            const int MOVEFILE_COPY_ALLOWED = 0x00000002;
            const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004;
            const int MOVEFILE_WRITE_THROUGH = 0x00000008;
            const int MOVEFILE_CREATE_HARDLINK = 0x00000010;
            const int MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020;
            [DllImport("kernel32.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
            static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);
            static void Main(string[] args)
                if (MoveFileEx(@"C:\Windows\System32\drivers\etc\hosts",
    0 comments No comments