2012-12-21 1 views
-1

C#에서 hostfile을 사용하면 웹 사이트를 차단할 수 있지만 차단 해제 할 수는 없습니다.특정 웹 사이트 차단 해제

String path = @"C:\Windows\System32\drivers\etc\hosts"; 
StreamWriter sw = new StreamWriter(path, true); 
sitetoblock = "\r\n127.0.0.1\t" + txtException.Text; 
sw.Write(sitetoblock); 
sw.Close(); 

MessageBox.Show(txtException.Text + " is blocked", "BLOCKED"); 
lbWebsites.Items.Add(txtException.Text); 
txtException.Clear(); 

여기에 내가 목록 상자 (lbWebsites)에서 선택되는 특정 사이트를 차단하는 몇 가지 도움이 필요합니다. 호스트 파일에서 제거 할 수 있습니까? 나는 많은 노력을 기울 였고 다른 솔루션을 찾았지만 모든 솔루션에서 문제가 발생했습니다.

+3

이 가능합니다. 파일을 읽고 차단 해제하고 다시 쓰려는 IP를 제거하십시오. "무언가 잘못되었다"는 것은 무엇을 의미합니까? – PhoenixReborn

+0

문제가 생겼습니다 - 쓰기 권한이 있습니까? – fableal

+0

호스트 파일을 사용하여 사이트를 차단하는 것이 바람직한 방법은 아닙니다. 이렇게하려면 내장 된 Windows 방화벽을 사용하십시오.) 로컬 프록시를 설치 한 경우를 상상해보십시오.) –

답변

1

호스트 파일을 string으로 읽으려면 StreamReader을 사용할 수 있습니다. 그런 다음 차단 해제 할 웹 사이트를 제외하고 다시 수집 된 콘텐츠를 작성하려면 StreamWriter의 새 인스턴스를 초기화하십시오.

string websiteToUnblock = "example.com"; //Initialize a new string of name websiteToUnblock as example.com 
StreamReader myReader = new StreamReader(@"C:\Windows\System32\drivers\etc\hosts"); //Initialize a new instance of StreamReader of name myReader to read the hosts file 
string myString = myReader.ReadToEnd().Replace(websiteToUnblock, ""); //Replace example.com from the content of the hosts file with an empty string 
myReader.Close(); //Close the StreamReader 

StreamWriter myWriter = new StreamWriter(@"C:\Windows\System32\drivers\etc\hosts"); //Initialize a new instance of StreamWriter to write to the hosts file; append is set to false as we will overwrite the file with myString 
myWriter.Write(myString); //Write myString to the file 
myWriter.Close(); //Close the StreamWriter 

감사합니다,
난 당신이 :)이 도움이

+0

많은 도움을 주셔서 감사합니다. – user1913674

+0

@ user1913674 도와 드릴 수있어서 기쁩니다. [이미 문제를 해결했음을 나타 내기 위해 게시물을 답변으로 표시 할 수 있습니다 (http://i.stack.imgur.com/uqJeW.png). 좋은 하루 되세요 :) –

+1

+1. '.Close' 대신에'using() {...}'을 사용하는 것이 안전하고 (''try' /'finally'' 이미 샘플이 없습니다.), 읽기/확인하기 쉽습니다. –

3

사이트를 차단하기 위해 작성한 행을 삭제해야합니다. 가장 효과적인 방법은 호스트 파일을 읽고 다시 쓰는 것입니다.

현재 사이트를 차단하는 방법은별로 효과가 없을 것입니다. 그것은 당신의 사용 시나리오 괜찮을 수도 있지만 약간 기술 사람들은 호스트 파일을보고 알게 될 것이다.

+1

+1; Windows 8 Smart Screen은 또한 공격에 널리 사용되기 때문에 HOSTS 파일에 대한 변경 사항을 적극적으로 되돌립니다. – vcsjones

0

당신은이 작업을 수행 할 수 있습니다되기를 바랍니다 : 다음

String path = @"C:\Windows\System32\drivers\etc\hosts"; 
System.IO.TextReader reader = new StreamReader(path); 
List<String> lines = new List<String>(); 
while((String line = reader.ReadLine()) != null) 
    lines.Add(line); 

당신은 당신의 호스트의 모든 라인을 가지고 - 행 목록의 파일. 원하는 사이트가 목록에 더 이상없는 때까지 그 후, 당신은 당신이 차단을 해제하고 목록에서 제거 할 사이트를 검색 할 수 있습니다

int index = 0; 
while(index != -1) 
{ 
    index = -1; 
    for(int i = 0; i< lines.Count(); i++) 
    { 
     if(lines[i].Contains(sitetounblock)) 
     { 
      index = i; 
      break; 
     } 
    } 
    if(index != -1) 
     lines.RemoveAt(i); 
} 

이 있음을 완료 한 후, 바로 정상에 청소 목록을 변환 문자열 :

String content = ""; 
foreach(String line in lines) 
{ 
    content += line + Environment.NewLine; 
} 

가 그럼 그냥 파일에 내용 쓰기 없습니다 오류가없는에

내 머리로 작성) 그래서 보장 : P 물론

관련 문제