Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using below code to unblock website but it does not work . please help me. how to unblock websites.



C#
public bool unblockwebsite(string strurlunblock)
       {
           try
           {
               string tempFile = Path.GetTempFileName();
               string hostpath = Environment.GetEnvironmentVariable("windir") + "\\System32\\drivers\\etc\\hosts";
               using (StreamReader sr = new StreamReader(hostpath))
               {
                   using (StreamWriter sw = new StreamWriter(tempFile))
                   {
                       string line;

                       while ((line = sr.ReadLine()) != null)
                       {
                           if (line != " 127.0.0.1" + " " + strurlunblock)
                               sw.WriteLine(line);
                       }
                       sw.Close();
                   }
                   sr.Close();
               }

               //File.Delete(hostpath);
               //File.Move(tempFile, hostpath);

               if (File.Exists(tempFile))
                   File.Copy(tempFile, hostpath, true);
            return true;
           }
           catch
           {
               return false;
           }
       }
Posted
Updated 12-Sep-12 3:56am
v2
Comments
[no name] 12-Sep-12 9:58am    
Do you think that it's possible that your idea of "but it does not work " might be vastly different than everyone else's idea of "but it does not work " means? What exactly does not work? Do you get an error? What error did you get? Does the file disappear? Does your hard drive melt? Did your cat die?
[no name] 12-Sep-12 10:08am    
I wish I could vote 5 your comment.
pradeep rasara 13-Sep-12 1:26am    
yes file disappear.
pradeep rasara 13-Sep-12 2:36am    
host file get deleted automatically.........plz help me.
ZurdoDev 12-Sep-12 10:21am    
What part is not working?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900