2014-01-17 48 views
0

我已經生成了一個控制檯應用程序,並嘗試通過向其傳遞參數來使用批處理文件運行控制檯應用程序。當我嘗試運行批處理文件時,出現如下錯誤。但是,當我在命令提示符中導航到應用程序位置並傳遞參數時,應用程序運行正常。找不到路徑問題的一部分

C:\WINDOWS\system32>"C:\Users\Akgem\Desktop\Infos\Logs.exe" "1.2.0.2" 
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\WIND 
OWS\system32\Infos\LogInfo.log'. 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I 
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o 
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolea 
n useLongPath, Boolean checkHost) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, 
FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean 
bFromProxy, Boolean useLongPath, Boolean checkHost) 
    at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean che 
ckHost) 
    at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encodin 
g, Int32 bufferSize, Boolean checkHost) 
    at System.IO.File.InternalWriteAllText(String path, String contents, Encoding 
encoding, Boolean checkHost) 
    at System.IO.File.WriteAllText(String path, String contents) 
    at GatherLogs.Program.Logentries(String text) 
    at GatherLogs.Program.Main(String[] args) 

Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\WIND 
OWS\system32\Infos\LogInfo.log'. 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I 
nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o 
ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolea 
n useLongPath, Boolean checkHost) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, 
FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean 
bFromProxy, Boolean useLongPath, Boolean checkHost) 
    at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean che 
ckHost) 
    at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encodin 
g, Int32 bufferSize, Boolean checkHost) 
    at System.IO.File.InternalWriteAllText(String path, String contents, Encoding 
encoding, Boolean checkHost) 
    at System.IO.File.WriteAllText(String path, String contents) 
    at GatherLogs.Program.Main(String[] args) 

批處理文件內容爲:

"%~dp0Logs.exe" "1.2.0.2" 
pause 

誰能幫我解決這個問題?

在此先感謝。

+0

找不到路徑'C:\ WINDOWS \ system32'的一部分\相關信息\ LogInfo.log」。意味着沒有找到該目錄。在發佈之前,請自己閱讀整個堆棧跟蹤。 – Peter

+0

您能否顯示正在執行的代碼? –

+0

@Peer試圖從桌面位置(C:\ Users \ Akgem \ Desktop \ Infos \ Logs.exe)運行開發的應用程序。而不是採取路徑(C:\ Users \ Akgem \ Desktop \ Infos \\ LogInfo.log),檢查'C:\ WINDOWS \ system32 \ Infos \ LogInfo.log位置中的日誌文件。這是什麼混淆和創建問題在這裏 –

回答

2

您的當前工​​作目錄是C:\WINDOWS\system32,如命令行顯示。顯然你的應用程序預計工作目錄是C:\Users\Akgem\Desktop\Infos\(或者只是C:\Users\Akgem\Desktop)。因此,你應該執行程序前切換到該目錄:

cd "%~dp0" 
Logs.exe "1.2.0.2" 
+0

是的。你絕對正確。我曾經使用過相同的批處理文件內容,但之前運行的是我開發的應用程序。它工作得很好。現在,我正在得到錯誤。我是否需要在批處理文件參數中進行更改? –

+0

@ user2505309,是的,你應該在批處理中運行這個'cd'命令,這樣當你運行該程序時,它將在正確的工作目錄中執行。你可以使用'cd%〜dp0',我想不是指定整個路徑,或者使用pushd/popd(例子[here](http://stackoverflow.com/questions/9597001/how-to-set-the-working - windows-batch-file -directory-of-command-in-windows-batch-file)) – Andrei

+0

@ user2505309,用什麼批處理文件看起來應該是什麼樣子 – Andrei

0

爲您的堆棧跟蹤顯示您正在傳遞給方法的路徑

GatherLogs.Program.Logentries(字符串文本)

到寫入文本無效並且不存在請確保首先在方法Logentries(String text)中存在「LogInfo.log」的路徑

相關問題