C
C#2mo ago
VRose

Modifying the hosts file through c# code

I've been trying to modify the hosts file in Windows using C# .NET windows desktop app, but no matter what I try, I always get the error Access denied even though I do run my code as admin. I've tried : 1. manually opening the hosts file and writing to it 2. running a cmd script as admin (that usually works when manually ran) but it didnt work when I called it from my code 3. running a ps script (that also works when ran manually) but didn't work when ran inside my c# code. So how can I do this..? Seems like I just can't modify the hosts file at all in C# even with admin privileges
4 Replies
viceroypenguin
viceroypenguin2mo ago
your access is being blocked by "User Access Control". try using hte "Run as Administrator" option in the start menu to get a command prompt that has the necessary access.
VRose
VRose2mo ago
I did that but I still got the same error
viceroypenguin
viceroypenguin2mo ago
before i try to help you any further, why are you trying to modify the hosts file?
VRose
VRose2mo ago
I'm trying to make an app that basically restores some settings and environment in my computer and I want it to have a decent user interface so that's why I went with a windows app instead of just a cli app Please reply to my text next time so I get notified that you replied >.<