即使以管理员身份运行,我的程序也无法写入HOSTS文件

时间:2012-05-26 14:22:29

标签: c# hosts-file unauthorizedaccessexcepti

我有一个奇怪的问题。 我正在用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)

我还没有写清单文件。我正在使用默认的清单选项进行编译。 那么问题是什么?我该怎么办?

谢谢。

2 个答案:

答案 0 :(得分:3)

如果启用了UAC,即使您是管理员,也无法编写主机,除非您以提升模式启动程序以激活管理员权限。

有关代码中安全权限的信息,请参阅herehere

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>

答案 1 :(得分:1)

没关系。 我找到了罪魁祸首。 UAC的一切都很好,但我的卡巴斯基阻止了我的程序: - /

谢谢你,亚历山大的帮助。