Asp.net: Obtain the Console Application Path

Source: Internet
Author: User

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.

 

How to obtain the application path (SET) in C #
String str1 = process. getcurrentprocess (). mainmodule. filename; // obtain the file name of the currently executed exe.
String str2 = environment. currentdirectory; // gets and sets the fully qualified path of the current directory (that is, the directory from which the process starts.
// The remarks are as defined. if the process is started in the local or root directory of the network drive, the value of this attribute is the drive name followed by a backslash (such as "C: \"). If the process is enabled in a subdirectory, the value of this attribute is a drive and subdirectory path (such as "C: \ mysubdirectory") without a backslash ").
String str3 = directory. getcurrentdirectory (); // obtain the current working directory of the application.
String str4 = appdomain. currentdomain. basedirectory; // gets the base Directory, which is used by the Assembly conflict resolution program to detect the assembly.
String str5 = application. startuppath; // obtain the path of the executable file that started the application, excluding the name of the executable file.
String str6 = application. executablepath; // obtain the path of the executable file that started the application, including the name of the executable file.
String str7 = appdomain. currentdomain. setupinformation. applicationbase; // obtain or set the name of the directory containing the application.

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.

For Windows and Web applications, the path they run is different, so the key is to determine which program is currently running. so we can use the following code string Path = "";
If (system. environment. currentdirectory = appdomain. currentdomain. basedirectory) // Windows applications are equal
{
Path = appdomain. currentdomain. basedirectory;
}
Else
{
Path = appdomain. currentdomain. basedirectory + "bin \";
}
In this way, if we write a class library and use assembly. loadfrom in the class library, because it is a general class library, it may also be used in Windows programs, so it is very convenient to use the above Code.

1. server. mappath
2. system. Windows. Forms. startuppath
3. type. Assembly. Location

Method 2 can be applied to console applications, winform applications, Windows Services, method 1 can be applied to Web applications, and method 3 can be applied.
But method 3 is the path to load the application. For a web application, the obtained path is the c: \ windows \ Microsoft. NET \ framework \ v1.1.4322 \ temporary ASP. NET Files directory. Therefore, we recommend that you use server. mappath for web projects. Otherwise, method 2 is recommended. If you create a new class library. You can add a reference to system. Windows. Forms. startuppath for use.

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-& gt; appdomain-& gt; assembly is very helpful for implementing the correct logic.

Related Article

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.