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

View: 31124|Reply: 2

[Console Program] Be wary of System.Environment.CurrentDirectory to get the current directory

[Copy link]
Posted on 11/10/2020 3:04:17 PM | | | |
In my recent work, I want to make a small tool for client reminders; Naturally, the winform program must read the configuration file of the same directory as the application (not the exe.config file);

To read the directory where the current application is located, I immediately thought of System.Environment.CurrentDirectory to get the current working directory. The program seems to run flawlessly;

When I came to work the next morning, it popped up: "Read configuration file missing". The app adds boot auto-start. This message pops up to read the configuration file failure, immediately check whether the configuration file in the application directory has been deleted by itself. After checking, I found that the file was still there, and I exited the program and ran it again

Still fine, no pop-up read profile missing. Then see if the path of the boot entry in the registry is not in my directory? Looking at the path file, it's all right. A little confused? Log out of the computer and try to restart it still pops up the configuration file is missing. The reason for not being able to find the way will be taken directly

MessageBox, the program runs and finds that the path is indeed this path.

Add the MessageBox code and continue to boot the test. When I booted up, I found that the current directory that popped up was C:\Windows\System32, and I was puzzled at once. I thought to myself, could it be that Lao Tzu's program ran down to system32? Checking the registry and sysetm32 doesn't have my app. Finally, I thought that there should be a problem with the code to get the path.

After repeated testing, it was found that System.Environment.CurrentDirectory is indeed to get 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. It is not difficult to find that the directory that pops up when I boot is C:\Windows\System32 because the boot self-startup program is also called by a process in Windows.

Therefore, if the winform program wants to obtain the directory where the current application is located, it is best not to use System.Environment.CurrentDirectory. Instead, it is: Application.StartupPath This way, it doesn't matter who calls it.

Test code:




Transferred from:The hyperlink login is visible.




Previous:K8S network flannel and calico comparison
Next:C# puts reference library DLLs into subfolders via probing
 Landlord| Posted on 11/10/2020 3:08:26 PM |
C# is a collection of methods to get the current program running path

Get the full path to the current process, including the file name (process name).
string str = this. GetType(). Assembly.Location;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

The full path to the main module that gets the new Process component and associates it with the currently active process, including the file name (process name).
string str = System.Diagnostics.Process.GetCurrentProcess(). MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (.exe文件所在的目录)

Gets the base directory of the current application domain of the current Thread, which is used by the assembly conflict solver to probe the assembly.
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

Get and set the name of the directory that contains the application. (Recommended)
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

Get the path to the executable file that started the application, excluding the name of the executable.
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (.exe文件所在的目录)

//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

Get the current working directory of the application (unreliable).
string str = System.IO.Directory.GetCurrentDirectory();
result: X:\xxx\xxx (.exe文件所在的目录)
Posted on 9/22/2021 8:38:52 PM |
Learn to learn...
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