2016-08-20 21 views
-1

Visual Studioでプログラムを実行すると、管理者権限なしでも完全に動作します。しかし、私が.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() 
+0

こんにちはYohanさん、よろしくお願いします。実際に '"'を使用しているのでしょうか、実際にそれらの場所に普通の普通の二重引用符 '' 'がありますか? –

+0

ただ、' '' @Vince Bowdren –

答えて

0

どのような種類のスパイウェア対策プログラムがインストールされていますか?ホストファイルが変更されないようにするものもあります。この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, ForAppending) 
objFile.WriteLine(vbNewLine & "127.0.0.1 www.google.lk") 
objFile.Close 

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

同じ文字列を削除するコード。

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 
+0

このコードを追加する必要がありますか? @ランディシューマン –

+0

Pls助けてください、私はVB.NETよくわからない..これは私の割り当てです.. pls plsヘルプ@ランディシューマン –

+0

これはVBscriptではなく、VB.netです。このコードを.vbs拡張子のファイルに保存するだけで、バッチファイルや他のプログラムやインストーラから%windir%\ system32 \ cscript.exe filename.vbs https://technet.microsoft.comを使用して呼び出すことができます.com/ja-us/library/ee156587.aspx –

0

-

は、ここに私のコードです。

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 
+0

私は**管理者**として実行しようとしました** **実行レベル** ** "asInvoker"から "highestAvailable" ** ** app.manifes ** tファイルに変更しました。しかし、まだ_アクセスが拒否されました_エラーメッセージ。 _hosts_ファイルを編集する他の方法はありますか?または_hosts_へのアクセス権を取得する方法はありますか? Plsは私を助ける... @ Ndls –

関連する問題