Vb.net 对主机文件的访问被拒绝

Vb.net 对主机文件的访问被拒绝,vb.net,vbscript,Vb.net,Vbscript,当我在VisualStudio中运行我的程序时,它在没有管理员权限的情况下可以完美地工作。但当我使用.exe文件运行程序时,出现了错误-访问路径C:/Windows/System32/drivers/etc/host我试图以管理员身份运行它,但根本不起作用 这是我的密码- Dim file As System.IO.StreamWriter file = My.Computer.FileSystem.OpenTextFileWriter("C:/Windows/System32/drivers/

当我在VisualStudio中运行我的程序时,它在没有管理员权限的情况下可以完美地工作。但当我使用.exe文件运行程序时,出现了错误-访问路径
C:/Windows/System32/drivers/etc/host
我试图以管理员身份运行它,但根本不起作用

这是我的密码-

Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("C:/Windows/System32/drivers/etc/hosts", True)
file.WriteLine("127.0.0.1 " + www.google.lk)
file.Close()

如您所见,只有管理员可以对其进行写入

 C:\Windows\system32>icacls "C:\Windows\System32\drivers\etc\hosts"

 C:\Windows\System32\drivers\etc\hosts NT AUTHORITY\SYSTEM:(I)(F)

                                       BUILTIN\Administrators:(I)(F)

                                       BUILTIN\Users:(I)(RX)

                                       APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES:(I)(RX)

 Successfully processed 1 files; Failed processing 0 files

您是否安装了任何类型的反间谍软件程序?有些会阻止修改主机文件。这个vbscript适合我。删除只读属性,执行编辑并重新应用只读属性。因为它有一些重复的代码,所以可能应该对它进行修改,使其具有更“合适”的功能

删除相同字符串的代码

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Const ForReading =   1
Const ForWriting =   2
Const ForAppending = 8
Const ReadOnly =     1

strWinDir = WshShell.ExpandEnvironmentStrings("%windir%")
HostsFile = strWinDir & "\System32\drivers\etc\hosts"

Set objFile = objFSO.GetFile(HostsFile)
If objFile.Attributes AND ReadOnly Then
  objFile.Attributes = objFile.Attributes XOR ReadOnly
End If

Set objFile = objFSO.OpenTextFile(HostsFile, ForReading)

Do Until objFile.AtEndOfStream
  strLine = objFile.ReadLine
    If InStr(strLine, "127.0.0.1 www.google.lk") = 0 Then
      strContents = strContents & strLine & vbCrLf
    End If
Loop
objFile.Close

Set objFile = objFSO.OpenTextFile(HostsFile, ForWriting)
objFile.Write strContents
objFile.Close

Set objFile = objFSO.GetFile(HostsFile)
If Not objFile.Attributes AND ReadOnly Then
  objFile.Attributes = objFile.Attributes XOR ReadOnly
End If

是的!我尝试以管理员身份运行,并在app.manifest文件中将请求的执行级别从“asInvoker”更改为“highestAvailable”。但仍然收到“访问被拒绝”错误消息。是否有其他方法来编辑主机文件?或者以任何方式获得访问主机的权限?请帮助我@帮我个忙,我对VB.NET不太了解。。这是我的任务。。请帮助@Randy Schumanth这是VBscript,不是vb.net。只需将所有这些代码保存到扩展名为.vbs的文件中,然后就可以使用批处理文件调用它,或者使用%windir%\system32\cscript.exe filename.vbs从其他程序或安装程序调用它。哦,是的,我理解!非常感谢,它工作得很好。顺便问一下,还有一件事,我如何使用VB脚本从主机文件中删除“127.0.0.1 www.google.lk”??如果可能的话,我可以这样解除网站的封锁@Randy Schumanokay我做了标记!我可以通过防病毒软件防止vbs文件检测为恶意软件吗@感谢Randy Schumanthanx@Randy Schuman帮助我。你真是个天才!!thanx againHi Yohan,欢迎来到Stack Overflow。我可以检查一下你的代码吗?它真的使用了
”还是在那些地方有一个普通的双引号
?是的,只有
@Vince Bowdren
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Const ForReading =   1
Const ForWriting =   2
Const ForAppending = 8
Const ReadOnly =     1

strWinDir = WshShell.ExpandEnvironmentStrings("%windir%")
HostsFile = strWinDir & "\System32\drivers\etc\hosts"

Set objFile = objFSO.GetFile(HostsFile)
If objFile.Attributes AND ReadOnly Then
  objFile.Attributes = objFile.Attributes XOR ReadOnly
End If

Set objFile = objFSO.OpenTextFile(HostsFile, ForReading)

Do Until objFile.AtEndOfStream
  strLine = objFile.ReadLine
    If InStr(strLine, "127.0.0.1 www.google.lk") = 0 Then
      strContents = strContents & strLine & vbCrLf
    End If
Loop
objFile.Close

Set objFile = objFSO.OpenTextFile(HostsFile, ForWriting)
objFile.Write strContents
objFile.Close

Set objFile = objFSO.GetFile(HostsFile)
If Not objFile.Attributes AND ReadOnly Then
  objFile.Attributes = objFile.Attributes XOR ReadOnly
End If