Quiero que los cambios sean permanentes en la PC
Acabo de codear una Class para manipular el archivo Hosts, puedes agregar un nuevo mapeo, bloquear diréctamente una url, eliminar un mapeo ...sólamente lo he testeado lo necesario.
Aquí tienes
:
#Region " Hosts Helper "
Public Class Hosts_Helper
' [ Hosts Helper ]
'
' // By Elektro H@cker
'
' Examples:
'
' MsgBox(Hosts_Helper.HOSTS_Exists)
' Hosts_Helper.Add("www.youtube.com", "231.7.66.33")
' Hosts_Helper.Block("www.youtube.com")
' MsgBox(Hosts_Helper.IsAdded("www.youtube.com"))
' MsgBox(Hosts_Helper.IsBlocked("www.youtube.com"))
' Hosts_Helper.Remove("www.youtube.com")
' Hosts_Helper.Clean_Hosts_File()
Shared ReadOnly HOSTS As String = _
IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Drivers\etc\hosts")
''' <summary>
''' Adds a new Block mapping into the Hosts file.
''' </summary>
Public Shared Sub Block(ByVal URL As String)
Dim Entry As String = String.Format("::1 {0}", URL)
If HOSTS_Exists() AndAlso IsBlocked(URL) Then
Throw New Exception(String.Format("""{0}"" is already blocked.", URL))
Exit Sub
ElseIf HOSTS_Exists() AndAlso IsAdded(URL) Then
Remove(URL)
End If
Try
IO.
File.
AppendAllText(HOSTS,
(Environment.
NewLine & Entry
), System.
Text.
Encoding.
Default) Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
''' <summary>
''' Adds a new mapping into Hosts file.
''' </summary>
Public Shared Sub Add(ByVal URL As String, ByVal IP_Address As String)
Dim Entry As String = String.Format("{0} {1}", IP_Address, URL)
If HOSTS_Exists() AndAlso (IsAdded(URL) OrElse IsBlocked(URL)) Then
Throw New Exception(String.Format("""{0}"" is already mapped.", URL))
Exit Sub
ElseIf Not Validate_IP(IP_Address) Then
Throw New Exception(String.Format("""{0}"" is not a valid IP adress.", IP_Address))
Exit Sub
End If
Try
IO.
File.
AppendAllText(HOSTS,
(Environment.
NewLine & Entry
), System.
Text.
Encoding.
Default) Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
''' <summary>
''' Removes a blocked or an added URL from the Hosts file.
''' </summary>
Public Shared Sub Remove(ByVal URL As String)
If Not HOSTS_Exists() Then
Throw New Exception("HOSTS File does not exists.")
Exit Sub
ElseIf HOSTS_Exists() And Not (IsAdded(URL) OrElse IsBlocked(URL)) Then
Throw New Exception(String.Format("""{0}"" is not added yet.", URL))
Exit Sub
End If
Try
Dim Content As String = _
System.
Text.
RegularExpressions.
Regex.
Replace(IO.
File.
ReadAllText(HOSTS
).
ToLower, _
String.Format("(\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}}|::1)(\s+|\t+){0}", URL.ToLower), String.Empty)
IO.
File.
WriteAllText(HOSTS, Content, System.
Text.
Encoding.
Default)
Catch ex As Exception
Throw New Exception(ex.Message)
End Try
End Sub
''' <summary>
''' Checks if an URL is already added into the Hosts file.
''' </summary>
Public Shared Function IsAdded(ByVal URL As String) As Boolean
Return If(Not HOSTS_Exists(), False, _
System.Text.RegularExpressions.Regex.IsMatch( _
System.
Text.
RegularExpressions.
Regex.
Replace(IO.
File.
ReadAllText(HOSTS
).
ToLower,
"\s+|\t+",
";"), _
String.Format(";[^\#]?\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}}\.\d{{1,3}};{0}", URL.ToLower)))
End Function
''' <summary>
''' Checks if an URL is already blocked into the Hosts file.
''' </summary>
Public Shared Function IsBlocked(ByVal URL As String) As Boolean
Return If(Not HOSTS_Exists(), False, _
System.Text.RegularExpressions.Regex.IsMatch( _
System.
Text.
RegularExpressions.
Regex.
Replace(IO.
File.
ReadAllText(HOSTS
).
ToLower,
"\s+|\t+",
String.
Empty), _
String.Format("[^\#](127.0.0.1|::1){0}", URL.ToLower)))
End Function
''' <summary>
''' Checks if the Hosts file exists.
''' </summary>
Public Shared Function HOSTS_Exists() As Boolean
Return IO.
File.
Exists(HOSTS
) End Function
''' <summary>
''' Cleans all the mappings inside the Hosts file.
''' </summary>
Public Shared Sub Clean_Hosts_File()
Try
IO.
File.
WriteAllText(HOSTS,
String.
Empty) Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
' Validates an IP adress.
Private Shared Function Validate_IP(ByVal IP_Address As String) As Boolean
Dim IP As System.Net.IPAddress = Nothing
Return System.Net.IPAddress.TryParse(IP_Address, IP)
End Function
End Class
#End Region
De nada