我已经生成了一个控制台应用程序,并试图通过将参数传递给它来使用批处理文件来运行控制台应用程序。当我试图运行批处理文件时,我收到如下错误。但是,当我在命令提示符中导航到应用程序位置并传递参数时,应用程序运行正常。
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 :(得分:2)
您当前的工作目录为C:\WINDOWS\system32
,命令行显示。显然,您的应用程序希望工作目录为C:\Users\Akgem\Desktop\Infos\
(或仅C:\Users\Akgem\Desktop
)。因此,您应该在执行程序之前更改到此目录:
cd "%~dp0"
Logs.exe "1.2.0.2"
答案 1 :(得分:0)
,因为您的堆栈跟踪显示了您传递给方法的路径
GatherLogs.Program.Logentries(String text)
写文本无效且不存在请首先确保方法Logentries(String text)中的“LogInfo.log”存在路径