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--
Unable to access and block url with Host file
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:
Also, please confirm if the above format is correct for blocking the URLs in the host file.
2 additional answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 61,731 Reputation points
2023-04-20T16:16:20.9866667+00:00 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.
-
RLWA32 43,306 Reputation points
2023-04-24T15:59:22.72+00:00 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", @"C:\Windows\System32\drivers\etc\hosts.bak", MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT)) { MoveFileEx(@"C:\Windows\System32\drivers\etc\hosts.new", @"C:\Windows\System32\drivers\etc\hosts", MOVEFILE_REPLACE_EXISTING | MOVEFILE_DELAY_UNTIL_REBOOT); } } }