0

I have generated a console application and tried to run the console application using batch file by passing the arguments to it. When i tried to run the batch file, i got an error like below. But, the application runs fine when i navigate to the application location in command prompt and pass the argument.

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)

Batch file content is :

"%~dp0Logs.exe" "1.2.0.2"
pause

Could anyone please help me to resolve the problem?

Thanks in Advance.

  • Could not find a part of the path 'C:\WINDOWS\system32\Infos\LogInfo.log'. Means that the directory is not found. Please read the whole stacktrace yourself before posting. – Peter Jan 17 '14 at 12:07
  • Can you please show the executing code? – Matan Shahar Jan 17 '14 at 12:07
  • @Peer am trying to run the developed application from the desktop location(C:\Users\Akgem\Desktop\Infos\Logs.exe). Instead of taking the path(C:\Users\Akgem\Desktop\Infos\\LogInfo.log), checking the log file in the 'C:\WINDOWS\system32\Infos\LogInfo.log location. This is what confusing and creating the problem here – VADIVEL NATARAJAN user2505309 Jan 17 '14 at 12:11
  • @user2505309 You should post the code from logs.exe, now we can only guess what is wrong. – Peter Jan 17 '14 at 12:12

2 Answers2

2

Your current working directory is C:\WINDOWS\system32, as the command line reveals. Apparently your application expects working directory to be C:\Users\Akgem\Desktop\Infos\ (or just C:\Users\Akgem\Desktop). Therefore you should change to this directory before executing a program:

cd "%~dp0"
Logs.exe "1.2.0.2"
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • Yes. You are absolutely correct. i have used the same batch file content but different version previously to run the app i developed. it worked fine. Now, am getting the error. Do i need to do change in batch file arguments? – VADIVEL NATARAJAN user2505309 Jan 17 '14 at 12:15
  • @user2505309, yes, you should run this `cd` command in the batch, so that when you run the program it is executed in the correct working directory. You can use `cd %~dp0` i think, not specifying the whole path, or use pushd/popd (examples [here](http://stackoverflow.com/questions/9597001/how-to-set-the-working-directory-of-a-command-in-windows-batch-file)) – Andrei Jan 17 '14 at 12:23
  • @user2505309, updated the post with what batch file should look like – Andrei Jan 17 '14 at 12:36
0

as your Stack trace shows the path your are passing to the method

GatherLogs.Program.Logentries(String text)

to Write the Text is not valid and does not exists please ensure first the path is exist for "LogInfo.log" within method Logentries(String text)

Pritam
  • 322
  • 2
  • 10