Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When I run my program in Visual Studio it works perfectly without admin privileges. But when I run program using .exe file error occurred - Access to path "C:/Windows/System32/drivers/etc/host"

Here's my code -

C#
Dim file As System.IO.StreamWriter
               file =    My.Computer.FileSystem.OpenTextFileWriter("C:/Windows/System32/drivers/etc/hosts", True)
               file.WriteLine("127.0.0.1 " + www.google.lk)
               file.Close()


What I have tried:

Tried running as Administrator
Change "asInvoker" to "highestAvailable" in app.manifest file
Tried giving access permission
Posted
Updated 22-Aug-16 21:48pm

1 solution

That file is protected by Windows. You need Admin permissions. Likely, you have to remove some flags like read-only before you can write to it.
Also note that Windows uses "virtualization" when an application tries to write where it must not do so. Take a look at C:\Users\Your.Username\AppData\Local\VirtualStore.
By the way, file.WriteLine("127.0.0.1 " + www.google.lk) is likely wrong, I guess it should be file.WriteLine("127.0.0.1 www.google.lk") if you want to redirect any calls to the sri lankan google site to localhost.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900