This article is a mirror article of machine translation, please click here to jump to the original article.

View: 50975|Reply: 0

[Source] .NET Core releases a single file path issue

[Copy link]
Posted on 2021-3-28 13:56:32 | | | |
Using .NET Core 3.1 to release a single-file console application, running on a Linux system, the main thing the program code does is very simple, that isRead the configuration file output in the program directory! However, when you publish a single file to run on Linux, it says that the file is not found.

The new .NET Core features release a single-file executable
https://www.itsvse.com/thread-8273-1-1.html

Be wary of System.Environment.CurrentDirectory to get the current directory
https://www.itsvse.com/thread-9464-1-1.html
Test code:

The output is as follows:

AppDomain.CurrentDomain.SetupInformation.ApplicationBase:/var/tmp/.net/root/ImageWatermark/e5snojro.qyv/
Environment.CurrentDirectory:/root/test
AppDomain.CurrentDomain.BaseDirectory:/var/tmp/.net/root/ImageWatermark/e5snojro.qyv/


When publishing a .NET Core application as a single file, running it on a CentOS system, the program is self-extracted and released/var/tmp/.net/root/directory, and then call the program released in the directory, which can also explain why the program cannot find the configuration file.

solution

Using System.Environment.CurrentDirectory to read the program path, System.Environment.CurrentDirectory is indeed getting the current directory, but if program A calls program B and program B uses System.Environment.CurrentDirectory to get the directory. Then the directory obtained in the B program is no longer the directory where the B application is located; It becomes the directory where A is located.





Previous:.NET Core Linux Error: Couldn't find a valid ICU package installed...
Next:.NET Core watermarking issues on Linux pictures
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com