C #, ASP. NET gets the absolute path of the current application

Source: Internet
Author: User

ASP. NET gets the absolute path of a file in the class library. This problem is often encountered in the beginner's time, often to check the forgotten, forget to check. Waste a lot of today specifically write an article, later to check here. Have time to remember to study this thing.

In the main program directory will not say Page.Server.MapPath ();

In the class library, AppDomain.CurrentDomain.SetupInformation.ApplicationBase This property plus the file's path relative to the root directory is OK.

        Static voidMain (string[] args) {            /*use Application.startuppath As for System.IO.Directory.GetCurrentDirectory, because the application has a current working directory, this working directory will change, not always the process              Startup directory (of course the default startup is the application directory).              * For example, if you open the command line (CMD), it will show a path, you will find that the path (usually C:\Documents and Settings\ user name, under XP) is not the Cmd.exe directory (Cmd.exe is under System32).              * and this current directory is easy to change, even when you use the File Browsing dialog box (such as open file, save file), this current directory will change 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 can work correctly if you do not modify it. */            //gets the full path of the current process Master moduleConsole.WriteLine (Process.getcurrentprocess ().            Mainmodule.filename); /*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 working directory during the run, then you can call System.IO.Path method to get the absolute path.             * But 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. */            //gets the fully qualified path to the current working directoryConsole.WriteLine (environment.currentdirectory); /*if it is in a Windows Forms application, the current app also behaves as a System.Windows.Forms.Application object, * Through its static properties Application.executablepath and             Application.startuppath, you can get the path to the executable file and the boot path. * (need to add reference System.Windows.Forms to call application)*/            //gets the path to the executable file that launched the application, including the name of the executable file. Console.WriteLine (System.Windows.Forms.Application.ExecutablePath); //gets the path to the executable file that launched the application, not including the name of the executable file. Console.WriteLine (System.Windows.Forms.Application.StartupPath); /*But if it is not in the Windows application, or in the library, even if the properties of the Application object can still be obtained, * also need to add System.Windows.Forms this assembly reference in the project, non- is not always convenient. At this time, * can be done by assembly static method, getcallingassembly or getexecutingassembly get the current execution of the assembly, * and then through assembly             The location of the class gets the position of the Assembly.             * 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, the assembly is added with a strong name, and the runtime is not required to be in the same directory as the. exe. */Console.WriteLine (assembly.getcallingassembly ().            Location); Console.WriteLine (assembly.getexecutingassembly ().            Location); /* . NET process starts, the AppDomain is created, all the assembly are load into an AppDomain, and the Setupinformation attribute is provided in the AppDomain, which can be obtained when the AppDomain starts             Information, so you can get the path to the current application by calling AppDomain.CurrentDomain.SetupInformation.ApplicationBase.             * After obtaining the required directory through the above method, you can call 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. */            string_path =AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //Absolute PathConsole.WriteLine (Path.GetFullPath (_path)); //Summary://development.            NET application, it is very helpful to understand the relationship of process/application->appdomain->assembly and to implement the correct logic. //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. Console.readkey (); }

This article original post: http://www.cnblogs.com/hb_cattle/archive/2010/03/05/1679478.html

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.