2014-01-17 51 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”的路径

相关问题