Good day.

I wrote a console application in C # that simply downloads a file from a resource to the folder specified in the config.

It turned out an interesting situation - I run (from Visual Studio) F5 (in debugging) - everything works fine, as expected. But if I launch via Ctrl + F5 - it writes "the указанный путь был использован при запуске cmd.exe в качестве текущей папки. cmd не поддерживает пути UNC ..." after which the program crashes, because It tries to read the configuration file NOT from the program directory but from the Windows folder.

enter image description here

I set the task through the Task Director , who runs the program once every 1 hour, and he starts it - but this error crashes and the program crashes.

It is interesting that if you simply launch a program through an exe-file, everything works fine.

Tell me how to make the program run through the scheduler normally?

Sincerely, L.

  • Have you tried to specify a local path, like "C: \ ..."? It seems to me that the process started from the task designer is not allowed to use network paths. - mals

2 answers 2

The application settings (if it is not app.config, but a separate file in which the application will save its settings) are best stored in the user’s directory, since in the application directory may not have the appropriate rights to write files.

The user directory, where the application exactly has write permissions, can be obtained as follows:

 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 

But you need to keep in mind that under another Windows user it will be a different directory.

  • Wonderful option. Alternatively, you can still use Assembly.GetExecutingAssembly () - Leonard Bertone

Try to use AppDomain.CurrentDomain.BaseDirectory to form a path.

This will give you back the path to the directory where your exe is really located, and not where its working folder is.