C #, ASP. NET gets the absolute path of the current application, gets the program working path (reprint)

Source: Internet
Author: User

C #, ASP. NET gets the absolute path of the current application, gets the program working path ============================================ using Application.startuppath

As for
System.IO.Directory.GetCurrentDirectory, because the application has a current working directory, the working directory will change, not always the program's startup directory (of course, the default startup is the application directory).
For example, if you open the command line (CMD), it will display a path and you will find that the path (usually the c:/documents and settings/user name, under XP) is not the Cmd.exe directory (Cmd.exe is under System32).
This current directory is easy to change, even when you use a file-browsing dialog box (such as opening a file, saving a file), and the current directory changes to its last selected directory.

The startup directory in the shortcut is set to 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 do not have to modify it to work properly, understand

The process object is in the. NET is represented in the System.Diagnostics.Process class by calling Process.getcurrentprocess (). Mainmodule.filename gets the file name of the EXE that is currently executing. But this method gets only the file name, if the program does not switch the working directory during the run, then you can call the System.IO.Path method to get the absolute path. However, the current directory can also be obtained through environment.currentdirectory, and many software will switch the working directory when using open dialog file, which makes this mechanism ineffective.

If you are in a Windows Forms application, The current application also behaves as a System.Windows.Forms.Application object, with its static properties Application.executablepath and Application.startuppath, which can get the path and startup path of the executable file.

But if not in Windows applications, or in the library, even if the properties of the Application object can still be obtained, but also need to add System.Windows.Forms this assembly reference in the project, very inconvenient. At this time, the assembly of the current execution can be obtained by assembly static method, getcallingassembly or getexecutingassembly, The location of the assembly is then obtained through the Assembly class.

However, when using assembly, you may encounter permissions problems, while assembly.getcallingassembly or assembly.getexecutingassembly may not get the location of the. exe file. In the GAC, a strong-named assembly is added, and the runtime is not required to be in the same directory as the. exe.

. NET process starts, the AppDomain is created, all the assembly are load into an AppDomain, and the Setupinformation property is provided in the AppDomain to get some information when the AppDomain is started. Therefore, you can get the path where the current application is located by calling AppDomain.CurrentDomain.SetupInformation.ApplicationBase.

After fetching the required directory through the above method, you can call the System.IO.Path method to get the file name, directory name, absolute path, and so on. Stop parsing the path string and use the System.IO.Path class instead.

Development. NET application, it is very helpful to understand the relationship of process/application->appdomain->assembly and to implement the correct logic.

Gets the N method of the program's 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;
The M method that is valid for the Web site application.
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 instead of Page.Server.MapPath () in ASP., it can be said that the way to get the path is. NET common approach to obtaining paths is recommended.

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.