簡體   English   中英

即使以管理員身份運行,我的程序也無法寫入HOSTS文件

[英]My Program cannot write HOSTS file even when run as Admin

我有一個奇怪的問題。 我正在用C#(。Net 4客戶端配置文件)編寫一個應用程序,它將更改HOSTS文件中的條目。 我向HOSTS文件的用戶帳戶添加了“完全訪問權限”,並且可以在普通文本編輯器中毫無問題地編輯文件。

當在Visual Studio 2010調試器中運行時(當選擇“啟用Visual Studio托管過程”時),該應用程序將工作。

當我在Visual Studio外部運行應用程序時,即使“以管理員身份運行”(!!!),也可以在嘗試編寫HOSTS文件時出現UnauthorizedAccessException。 為什么? 詳細信息沒有給我任何線索:

System.UnauthorizedAccessException was caught
  Message=Der Zugriff auf den Pfad "C:\Windows\System32\drivers\etc\hosts" wurde verweigert.
  Source=mscorlib
  StackTrace:
   bei System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   bei System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
   bei System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
   bei System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   bei System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   bei System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
   bei System.IO.File.WriteAllLines(String path, String[] contents)
   bei App.HostsFile.Save() in HostsFile.cs:Zeile 125.
   bei App.Actions.SaveHosts.Execute(Context ctxt) in Actions\SaveHosts.cs:Zeile 16.
  InnerException: 
   (there is none)

我尚未編寫清單文件。 我正在使用默認清單選項進行編譯。 那是什么問題呢? 那我該怎么辦?

謝謝。

如果啟用了UAC,即使您是管理員,也將無法編寫主機,除非您以提升模式啟動程序以激活管理員權限。

請參閱此處此處,以獲取有關代碼中的安全權限的信息。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

    namespace WriteToTheHosts
    {
        class Program
        {
            static void Main(string[] args)
            {
                var systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
                Console.WriteLine(systemPath);
                var path = Path.Combine(systemPath, @"drivers\etc\hosts");
                using (var stream = new StreamWriter(path, true, Encoding.Default))
                {
                    stream.WriteLine("#from .NET");
                }
                Console.ReadKey();

            }
        }
    }

還關閉項目設置中的默認清單(清單:“創建沒有清單的應用程序”),並編寫自己的清單,如第一個鏈接示例所示:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
   <assemblyIdentity version="1.0.0.0" processorArchitecture="X86" name="WriteToTheHosts" type="win32"/>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
      <security>
         <requestedPrivileges>
            <requestedExecutionLevel level="requireAdministrator"/>
         </requestedPrivileges>
      </security>
   </trustInfo>
</assembly>

沒關系。 我找到了罪魁禍首。 使用UAC一切都很好,但是我的卡巴斯基阻止了我的程序:-/

謝謝亞歷山大的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM