© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
5 replies
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
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Executing a jar file through c#
C#CC# / help
16mo ago
Running .jar file through c# on windows?
C#CC# / help
16mo ago
C# redirect after sending file through headers
C#CC# / help
3y ago
Modifying an .exe file
C#CC# / help
3y ago