Hi,
The approach you used first is simple and
correct !
The code works perfectly fine for me, I just had to make a small change.
The only thing is changed is the path : "C:\windows\system32\drivers\etc\hosts.txt", 'cuz there is no such file !
The correct path should be : "C:\windows\system32\drivers\etc\hosts"
Try it