2012-04-17 20 views
2

我已經寫了一個windows窗體(.net 4)應用程序,需要將文件寫入文件夾'C:\ Windows \ System32 \ dns'。當這行代碼運行時:將文件保存到'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)調試,該 應用程序或計算機(Machine.config中)的config文件必須有 jitDebugging值在system.windows.forms部分中設置。 該應用程序還必須在啓用調試 的情況下進行編譯。

例如:

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

啓用JIT調試後,任何未處理的異常 將被髮送到計算機 上註冊,而不是由此對話框處理的JIT調試器。

回答

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位重定向。

+0

是我運行Windows 64,我會檢查這些鏈接了,謝謝! – Darthtong 2012-04-17 11:44:55

+0

http://www.pinvoke.net/default.aspx/kernel32.wow64revertwow64fsredirection和http://www.pinvoke.net/default.aspx/kernel32.wow64disablewow64fsredirection應該讓你去。 – denied66 2012-04-17 11:57:09

+0

VB例如這裏... HTTP://www.vbforums.com/showthread.php T = 646033 – Darthtong 2012-04-17 14:11:55

0

你可以檢查目錄是否存在?由於system32是一個windows文件夾,要在其中創建文件和文件夾需要特殊的權限。運行win窗體應用程序的用戶是否有權在system32文件夾內創建文件夾/文件?

+1

他說,他跑作爲問題管理 – Jacxel 2012-04-17 11:24:42

+0

是目錄肯定是存在的,它的DNS服務器保留它的區域文件等我運行的應用程序作爲管理員用戶,以便應該有權限的目錄! – Darthtong 2012-04-17 11:28:09

相關問題