-1

I guess you know this file well, it's a file for binding some IP to hostnames.

C:\Windows\System32\Drivers\etc\hosts

Notepad can make changes on this file but C# Text-stream couldn't do anything on it and says access denied.

Any ideas why this is happening?

P.S: My user has enough rights to edit that file because I can edit it with Notepad, but my programm couldn't.

EDIT: One more thing, it doesn't work with users that are created in ActiveDirectory. users who are not from this computer. They're administrator but they are not the owner.

Could it be ownership problem?

Ali Bahrami
  • 5,935
  • 3
  • 34
  • 53

2 Answers2

1

UAC??? You need to run your app with elevated privs... also try saving your file you have edited in notepad? Does that really work?

John Sobolewski
  • 4,512
  • 1
  • 20
  • 26
  • 1
    @ALi If you are not running notepad with elevated privs you are not really overwriting the file, it could be writing to `%localappdata%\Virtual Store\Windows\System32\Drivers\etc\hosts` instead of the real file. – Scott Chamberlain Sep 11 '12 at 17:10
  • @HenkHolterman He could be using some kind of notepad replacement (like Notepad++ but I think that is visualization aware too) and calling it notepad. I just wanted to cover all the bases – Scott Chamberlain Sep 11 '12 at 17:12
  • @ScottChamberlain Good hint , but I should check it. – Ali Bahrami Sep 11 '12 at 17:18
  • @ScottChamberlain - No, virtualization only happens with programs that are not Vista/Win7 aware. (reposted due to bad typo) – H H Sep 11 '12 at 17:20
  • P.S: I don't use any notepad replacement. – Ali Bahrami Sep 11 '12 at 17:23
  • @HenkHolterman It's my understanding that virtualization only happens with programs that are not Vista/Win7 aware because those programs don't write to the virtualized directories. Is there more to it? – phoog Sep 11 '12 at 17:28
  • A Vista+ program that writs where it shouldn't gets an exception. That includes Notepad. – H H Sep 11 '12 at 20:19
0

Try to add manifest to your programm and request neccessary rights.