如何检查"主机"文件?
我有主持人:
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost
127.0.0.1 test1
127.0.0.6 test2
127.0.0.1 test3
我需要检查" 127.0.0.6 test2"在主机文件上,用于服务器游戏。
代码vb添加新行:
Process.Start("cmd", "/c echo 127.0.0.6 test2 >> %WINDIR%\System32\Drivers\Etc\Hosts")
答案 0 :(得分:0)
使用.NET
非常简单首先使用File.ReadAllText和String.Contains来检查
然后,如果需要,只需附加File.AppendText
https://msdn.microsoft.com/en-us/library/system.io.file.appendtext(v=vs.110).aspx
示例:
Public Class Test
Public Shared Sub Main()
Dim path As String = "C:\Windows\ System32\Drivers\Etc\Hosts"
If File.Exists(path) AndAlso Not File.ReadAllText(path).Contains(“127.0.0.6 test2”) Then
‘File exists and doesn’t have our string - let’s add it
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
End If
End Sub
结束班