繁体   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