Go to C # program path

Source: Internet
Author: User

C # It's really annoying to get the program path. I 've been confused twice and extracted from others' articles as follows:

 

C # program path
Recently, a small program needs to read the configuration file. I put the configuration file in the root directory of the program and use the relative directory in the code to access it. In the beginning, everything was fine and there was no problem.

Yesterday, after I added a data export function to the program, I couldn't read the configuration file. Very depressing. Because the configuration files loaded by the system can be read normally. However, it won't work if I read it again during running. After studying it for a long time, I found that it was because I used the open file dialog box when exporting data. The relative path is changed.

To sum up, you must use the code to retrieve the absolute path of the configuration file. You can use the following two static methods:
Application. executablepath includes the file name
Application. startuppath does not include the file name.

Not in Windows applications, or in libraries, even if the properties of the Application object are still available, you also need to add the system in the project. windows. the reference of forms assembly is very inconvenient. At this time, you can use the static method of assembly, getcallingassembly or getexecutingassembly to obtain the currently executed assembly, and then obtain the position of the Assembly through the location of the Assembly class.

The location of the hosts file. A strong name assemblyis defined in gac. it cannot be in the same directory as .exe at runtime.

. When the process starts, the appdomain will be created, and all the assemblies will be loaded to a certain appdomain, while the appdomain provides the setupinformation attribute to obtain some information at appdomain startup, therefore, you can call appdomain. currentdomain. setupinformation. applicationbase obtains the path of the current application.

After obtaining the required directory through the preceding method, you can call system. Io. Path to obtain the file name, directory name, and absolute path. Stop analyzing the path string, instead use the system. Io. Path class.

When developing applications under. net, understanding the relationship between process/Application-> appdomain-> assembly is very helpful for implementing the correct logic.

Program path processing in C #

The preceding program file is also a comparative requirement. For example, if I have a file named conn.xmllocated in the same directory of .exe, I need to read some configuration information from it during runtime.
  
The process object in. NET is represented by the system. Diagnostics. Process class. By calling process. getcurrentprocess (). mainmodule. filename, the file name of the currently executed EXE can be obtained. However, this method only gets the file name. If the working directory is not switched during the program running, you can call the system. Io. path method to obtain the absolute path. However, the current directory can also be obtained through environment. currentdirectory, and many software switches the working directory when opening a file using open dialog, which invalidates this mechanism.

In a Windows Forms Application, the current application is also represented as system. windows. forms. application object through its static property application. executablepath and application. startuppath to obtain the path and start path of the executable file.

But if it is not in a Windows application or in a library, even if the properties of the Application object are still available, you must add the system in the project. windows. the reference of forms assembly is very inconvenient. At this time, you can use the static method of assembly, getcallingassembly or getexecutingassembly to obtain the currently executed assembly, and then obtain the position of the Assembly through the location of the Assembly class.

The location of the hosts file. A strong name assemblyis defined in gac. it cannot be in the same directory as .exe at runtime.

. When the process starts, the appdomain will be created, and all the assemblies will be loaded to a certain appdomain, while the appdomain provides the setupinformation attribute to obtain some information at appdomain startup, therefore, you can call appdomain. currentdomain. setupinformation. applicationbase obtains the path of the current application.

After obtaining the required directory through the preceding method, you can call system. Io. Path to obtain the file name, directory name, and absolute path. Stop analyzing the path string, instead use the system. Io. Path class.

When developing applications under. net, understanding the relationship between process/Application-> appdomain-> assembly is very helpful for implementing the correct logic.

Summarize the methods for obtaining the current working directory and execution directory of the program in C #.

1. system. Diagnostics. process. getcurrentprocess (). mainmodule. filename
Obtain the complete path of the module.
2. system. environment. currentdirectory
Obtain and set the fully qualified directory of the current directory from which the process starts.
3. system. Io. Directory. getcurrentdirectory ()
Obtain the current working directory of the application. This is not necessarily the directory from which the program starts. It may be stored in c: \ www. This function may return c: \ Documents ents and Settings \ zyb \, or C: \ Program Files \ Adobe \. Sometimes I don't know what to return.
4. system. appdomain. currentdomain. basedirectory
Obtain the base Directory of the program.
5. system. appdomain. currentdomain. setupinformation. applicationbase
Obtain and set the name of the directory containing the application.
6. system. Windows. Forms. application. startuppath
Obtain the path of the executable file that started the application. The effect is the same as that of 2 and 5. Only 5 returns an extra "\" behind the string.
7. system. Windows. Forms. application. executablepath
Obtain the path and file name of the executable file that started the application. The effect is the same as that of 1.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/Houluoxuan_168/archive/2010/04/14/5486019.aspx

 

In addition, how to obtain the file name from the address string and record it as follows

String Spath = openfiledialog. filename;
Int allnum = Spath. length;
Int Pos = Spath. lastindexof (@"\");
Apppath = Spath. substring (0, POS); // file path (excluding the file name)
Apppath = Spath. substring (Pos, allnum); // file name (excluding path)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.