2

Requirement: In my .Net Core console application, I use Directory.GetParent(Directory.GetCurrentDirectory()) to retrieve the current parent folder of where my template file is stored.

Issue: After deploying on server, when I manually run the exe by double clicking it works fine. But when I try to run it with batch or task scheduler, the above statement return by local computers path, instead of the path on server.

Server Details: OS: Windows Server 2012 R2 Standard

Error Stack: Error Message : Could not find document StackTrace : at DocumentFormat.OpenXml.Packaging.OpenXmlPackage.OpenCore(String path, Boolean readWriteMode) at DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(String path, Boolean isEditable, OpenSettings openSettings) at CV_Export.Program.SearchAndReplace(String TemplatePath, String NewDocumentPath) in C:\Siddharth\Demo Codes.Net Core Demo\CV Export with API\CV_Export\Program.cs:line 163 TIme : 7/21/2021 4:41:53 PM

This error was generated when I tried to run the exe through batch and the path on the error is of my local system.

Thank you in advance for your help.

Siddharth Dinesh
  • 345
  • 4
  • 13

2 Answers2

0

By using Directory.GetParent(App.Domain.CurrentDomain.BaseDirectory) you can provide the relative path to the base directory of your application.

When you are using Directory.GetCurrentDirectory() you are providing the path from where you are running the application, which can be for instance C:\Windows\System32, and the parent directory will be C:\Windows instead of the parent directory of your .net application.

user3026017
  • 569
  • 4
  • 10
-1

GetCurrentDirectory()
Gets the current working directory of the application.

The working directory is an environment concept and is affected by where and how the application is executed, not where the file is logically located.

You can avoid this by using App.Domain.CurrentDomain.BaseDirectory() instead, that will force lookups for these resources to always be relative to the executable.

NOTE: In many cases it is still beneficial to use GetCurrentDirectory() as that allows your executable to be installed in a central location, but to allow different user or security contexts to access a different set of files.

If you stick with GetCurrentDirectory() here's some advice on how to configure it from the calling context:

Batch Files / Scripts

When executing from a batch file, the current environment context will default to be operating in the working directory that applies to the batch folder.

So in your batch, you need to change the working directory to the folder the exe is in first, if you used a UNC path or full path to access the exe, then it won't be running in that directory, instead if you use a cd command to change the directory first that will often do the trick.

Task Scheduler

Shortcuts

  • Similar to Scheduled Tasks, in shortcuts you can also specify the working directory:
    enter image description here
Chris Schaller
  • 13,704
  • 3
  • 43
  • 81