0

I just want to know how can I completely replace the user's hosts file with another one? Note: I wana give the user just my .exe compiled file(With my own hosts file attached to it) and after running the exe file the user's hosts file should be replaced with my own hosts file I attached to my exe file.

1 Answer 1

1

Simple way, you can use IO library.

        string path = "system32\\drivers\\etc\\hosts";
        string hostfile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), path);

        const string checkIP = "127.0.0.1 localhost";
        if (!File.ReadAllLines(hostfile).Contains(checkIP))
            File.AppendAllLines(hostfile, new string[] {checkIP});

Do not forget, your program must run with administrator privileges, otherwise you will get an UnauthorizedAccessException exception.

Sign up to request clarification or add additional context in comments.

1 Comment

I think this will "write" the "127.0.0.1 localhost" on users's hosts file. I don't want to edit modify the file. As I said I want to completely <replace> it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.