C #. ASP. NET: Obtain the absolute path of the current application and the working path of the application.

Source: Internet
Author: User
Use application. startuppath

As
System. io. directory. getcurrentdirectory does not work. Because the application has a current working directory, this working directory will change, not always the startup directory of the Program (of course, the default startup is the application directory ).
For example, if you open the command line (CMD), it will display a path, you will find this path (usually c: \ Documents and Settings \ User Name, under the Windows XP, it is not the. exeworkflow directory (.exe is under system32 ).
The current directory is easy to change. Even when you use the File browsing dialog box (such as opening a file or saving a file), the current directory will be changed to its last selected directory.

The startup directory in the shortcut is to set the working directory of the current application. By default, the startup directory of the newly created shortcut is the directory where the application is located, so you can work normally without modifying it.

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.

// Obtain n methods of the program working path.
String TMP = system. Diagnostics. process. getcurrentprocess (). mainmodule. filename;
TMP = system. environment. currentdirectory;
TMP = system. Io. Directory. getcurrentdirectory ();
TMP = system. Windows. Forms. application. startuppath;
TMP = system. Windows. Forms. application. executablepath;
// Valid M methods for website applications.
TMP = system. appdomain. currentdomain. basedirectory;
TMP = system. appdomain. currentdomain. setupinformation. applicationbase;
TMP = httpcontext. Current. Request. physicalapplicationpath;

Appdomain. currentdomain. setupinformation. applicationbase this method can be used in ASP. net to replace page. server. mappath (). It can be said that the method for obtaining the path is.. Net (recommended.

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.