C# 找不到路径问题的一部分

C# 找不到路径问题的一部分,c#,C#,我已经生成了一个控制台应用程序,并尝试通过向其传递参数来使用批处理文件运行控制台应用程序。当我试图运行批处理文件时,我得到了如下错误。但是,当我在命令提示符下导航到应用程序位置并传递参数时,应用程序运行良好 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:\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
谁能帮我解决这个问题


提前感谢。

如命令行所示,您当前的工作目录是
C:\WINDOWS\system32
。显然,您的应用程序希望工作目录是
C:\Users\Akgem\Desktop\Infos\
(或者只是
C:\Users\Akgem\Desktop
)。因此,在执行程序之前,应更改到此目录:

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

当堆栈跟踪显示传递给方法的路径时

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


要写入无效且不存在的文本,请首先确保方法Logentries(字符串文本)中的“LogInfo.log”路径存在。

找不到路径“C:\WINDOWS\system32\Infos\LogInfo.log”的一部分。表示找不到该目录。在发布之前,请自己阅读整个stacktrace。您能显示执行代码吗?@Peer我正在尝试从桌面位置(C:\Users\Akgem\desktop\Infos\Logs.exe)运行开发的应用程序。检查“C:\WINDOWS\system32\Infos\LogInfo.log”位置中的日志文件,而不是选择路径(C:\Users\Akgem\Desktop\Infos\\LogInfo.log)。这就是令人困惑和产生问题的原因here@user2505309你应该发布logs.exe中的代码,现在我们只能猜出哪里出了问题。是的。你完全正确。我以前使用过相同的批处理文件内容,但不同的版本来运行我开发的应用程序。它工作得很好。现在,我得到了错误。是否需要更改批处理文件参数?@user2505309,是的,您应该在批处理中运行此
cd
命令,以便在运行程序时在正确的工作目录中执行。你可以使用
cd%~dp0
我想,不指定整个路径,或者使用pushd/popd(示例)@user2505309,用批处理文件的样子更新帖子