【问题标题】:Saving a file to 'C:\Windows\System32\dns' causes 'Could not find a part of the path' error将文件保存到“C:\Windows\System32\dns”会导致“找不到路径的一部分”错误
【发布时间】:2012-04-28 17:29:48
【问题描述】:

我编写了一个 Windows 窗体 (.net 4) 应用程序,该应用程序需要将文件写入文件夹“C:\Windows\System32\dns”。这行代码运行时:

File.WriteAllText(dnsFileLocation, luxDns)

我收到以下错误,有人知道为什么吗?

注意:即使我以管理员身份运行应用程序也会发生这种情况!

*****异常文本** *****

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Windows\System32\dns\lux2.dns'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at 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)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding)
   at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding)
   at System.IO.File.WriteAllText(String path, String contents)
   at DnsServerUpdater.Main.Main_Load(Object sender, EventArgs e)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.261 (RTMGDR.030319-2600)
    CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
DnsServerUpdater
    Assembly Version: 1.0.0.0
    Win32 Version: 1.0.0.0
    CodeBase: file:///C:/web/dns/DnsServerUpdater.exe
----------------------------------------
Microsoft.VisualBasic
    Assembly Version: 10.0.0.0
    Win32 Version: 10.0.30319.1 built by: RTMRel
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.261 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.233 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Windows.Forms
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.235 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.276 built by: RTMGDR
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Runtime.Remoting
    Assembly Version: 4.0.0.0
    Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
    CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Remoting/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll

***** JIT 调试 ** ***** 要启用即时 (JIT) 调试,为此的 .config 文件 应用程序或计算机 (machine.config) 必须具有 在 system.windows.forms 部分设置的 jitDebugging 值。 应用程序还必须通过调试进行编译 已启用。

例如:

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

启用 JIT 调试后,任何未处理的异常 将发送到计算机上注册的 JIT 调试器 而不是被这个对话框处理。

【问题讨论】:

    标签: asp.net windows vb.net winforms dns


    【解决方案1】:

    你能检查目录是否存在吗?由于 system32 是一个 windows 文件夹,因此在其中创建文件和文件夹需要特殊权限。运行 win forms 应用程序的用户是否有权在 system32 文件夹中创建文件夹/文件?

    【讨论】:

    • 他说他在问题中以管理员身份运行
    • 是的,该目录确实存在,它是 DNS 服务器保存它的区域文件等的目录。我以管理员用户身份运行应用程序,所以应该有权限!
    【解决方案2】:

    您运行的是 64 位 Windows 吗?如果是,那么这可能是您的问题,因为您将被重定向到另一个目录。

    您需要实现
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa365743%28v=vs.85%29.aspxhttp://msdn.microsoft.com/en-us/library/windows/desktop/aa365745%28v=vs.85%29.aspx 为了暂时禁用 64 位重定向以访问该文件夹。

    【讨论】:

    猜你喜欢
    • 2011-09-30
    • 2015-01-28
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多