C # obtain the relative path

Source: Internet
Author: User
1. Obtain the path of the current file  1  . System. Diagnostics. process. getcurrentprocess (). mainmodule. filename: Obtain the complete path of the module, including the file name.  2  . System. environment. currentdirectory gets and sets the fully qualified directory of the current directory (the directory from which the process starts.  3  . System. Io. Directory. getcurrentdirectory () Get ApplicationProgram. 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 not necessarily return anything. This is the last operation directory of any application. For example, if you open the file E: \ doc \ my.doc in word, then, E: \ doc is returned.  4  . System. appdomain. currentdomain. basedirectory gets the base Directory of the program.  5 . System. appdomain. currentdomain. setupinformation. applicationbase obtains and sets the Directory Name of the application.  6  . System. Windows. Forms. application. startuppath obtains the executable file path of the application. The effect is the same as that of 2 and 5. Only 5 returns an additional string  "  \ "Only  7  . System. Windows. Forms. application. executablepath obtains the path and file name of the executable file starting the application. The effect is the same as that of 1. Ii. Operating environment variables the system. environment. getenvironmentvariable () method can be used to conveniently obtain system environment variables, such as system. environment. getenvironmentvariable (  "  Windir  " ) To obtain the path of the Windows System directory. The following are common environment variables: system. environment. getenvironmentvariable (  "  Windir  "  ); System. environment. getenvironmentvariable (  "  Include  "  ); System. environment. getenvironmentvariable (  "  TMP  "  ); System. environment. getenvironmentvariable (  " Temp  "  ); System. environment. getenvironmentvariable (  "  Path  "  ); System. environment. systemdirectory; C: /Windows/ At the end of the System32 directory, the variable value obtained by the above operations is pasted. In advance, I wrote a winform program, and the project file is stored in D: \ Visual Studio projects \ myapplication \ lifeassistant, the compiled file is in D: \ Visual Studio projects \ myapplication \ lifeassistant \ bin \ debug. The final result is as follows:  1 , System. Diagnostics. process. getcurrentprocess (). mainmodule. filename = D: \ Visual Studio projects \ myapplication \ lifeassistant \ bin \ debug \ lifeassistant.exe 2 , System. environment. currentdirectory = D: \ Visual Studio projects \ myapplication \ lifeassistant \ bin \ debug  3 , System. Io. Directory. getcurrentdirectory () = D: \ Visual Studio projects \ myapplication \ lifeassistant \ bin \ debug  1  Asp.net webform uses "request. physicalapplicationpath to obtain the physical path of the virtual directory of the site, and finally contains" \ ";  2 . C # Use A: "application. startuppath: gets the path of the directory where the current application is located, and does not contain "\"; B: "application. executablepath ": gets the path of the current application file, including the file name; C:" appdomain. currentdomain. basedirectory: gets the path of the directory where the current application is located, and finally contains "\"; D: "system. threading. thread. getdomain (). basedirectory: gets the path of the directory where the current application is located, and finally contains "\"; E: "environment. currentdirectory: gets the path of the current application, and does not contain "\"; F: "system. io. directory. getcurrentdirectory: gets the path of the current application, and does not contain "\";  3  . C # Use "appdomain. currentdomain. basedirectory or system. threading. thread. getdomain (). basedirectory. currentdirectory and system. io. directory. getcurrentdirectory will get the path of the "System32" Directory; if you want to use "application. startuppath or application. executablepath, You need to manually add the "system. windows. forms. DLL, and use"  Using  System. Windows. Forms "declares this reference; 4  . Obtain the system installation directory from the uninstall program: system. reflection. Assembly curpath = System. reflection. Assembly. getexecutingassembly ();  String Path = curpath. location; //  Obtain the path of the setuplibrary file of the installer class. Obtain the directory where the file path is located to obtain the directory of the installer;  4 , System. appdomain. currentdomain. basedirectory = D: \ Visual Studio projects \ myapplication \ lifeassistant \ bin \ debug \  5 , System. appdomain. currentdomain. setupinformation. applicationbase = D: \ Visual Studio projects \ myapplication \ lifeassistant \ bin \ debug \ 6 , System. Windows. Forms. application. startuppath = D: \ Visual Studio projects \ myapplication \ lifeassistant \ bin \ debug  7 , System. Windows. Forms. application. executablepath = D: \ Visual Studio projects \ myapplication \ lifeassistant \ bin \ debug \ lifeassistant.exe system. environment. getenvironmentvariable (  "  Windir  " ) = C: \ Windowssystem. environment. getenvironmentvariable (  "  Include  " ) = C: \ Program Files \ Microsoft Visual Studio. NET 2003 \ SDK \ v1. 1  \ Include \ system. environment. getenvironmentvariable (  "  TMP  " ) = C: \ docume ~ 1 \ Zhoufoxcn \ locals ~ 1  \ Tempsystem. environment. getenvironmentvariable (  "  Temp  " ) = C: \ docume ~ 1 \ Zhoufoxcn \ locals ~1  \ Tempsystem. environment. getenvironmentvariable (  "  Path  " ) = C: \ windows \ system32; C: \ WINDOWS; c: \ windows \ system32 \ WBEM; C: \ jdk1. 5.0 \ Bin; C: \ mysqlserver5. 0 \ Bin; C: \ Program Files \ symantec \ pcAnywhere \; C: \ Program Files \ Microsoft SQL Server \ 80  \ Tools \ binnc # Relative Path System Path  2007 - 12 - 22   09 : 53  //  Obtain the path of the executable file that started the application, excluding the name of the executable file.  String Str5 = Application. startuppath;  //  Obtain the name of the currently executed EXE file.  String Str1 = Process. getcurrentprocess (). mainmodule. filename;  //  Obtain and set the full path of the current directory (that is, the directory from which the process starts. The remarks are as defined. if the process is started locally or in the root directory of the network drive, the value of this attribute is the drive name followed by a backslash (for example, "C :\"). If the process is started in a subdirectory, the value of this attribute is a drive and subdirectory path (such as "C: \ mysubdirectory") without a backslash ").  String Str2 =Environment. currentdirectory;  //  Obtain the current working directory of the application.  String Str3 = Directory. getcurrentdirectory ();  //  Gets the base Directory, which is used by the Assembly conflict resolution program to detect the assembly.  String Str4 = Appdomain. currentdomain. basedirectory;  //  Obtain the path of the executable file that started the application, excluding the name of the executable file.  String Str5 = Application. startuppath;  // Obtain the path of the executable file that started the application, including the name of the executable file.  String Str6 = Application. executablepath;  //  Obtain or set the name of the directory containing the application.  String Str7 = Appdomain. currentdomain. setupinformation. applicationbase;  //  Example  Application. startuppath;  //  F: \ learning \ C # training \ win \ bin \ debug  //  Note that you need to add two \ Application. startuppath + "  \ 3.jpg  "  In C #, the relative path is  "  .  " And "  ..  "  Indicates,  "  .  "  Indicates the current directory,  "  .. "  Indicates the upper-level record. For example, if I use vs2005 in D: \ My Documents ents \ Visual Studio  2005  The \ projects directory creates a project named controls, that is, there is a controls folder in the projects folder, and the controls folder contains three files: the controls. sln controls folder fogulfstlawrence folder. D: \ My Documents \ Visual Studio  2005  \ Projects \ controls \ bin \ debugthis simple project can run the executable file controls.exe now I want D: \ My Documents ents \ Visual Studio  2005  The path to the gulf_of_st. _ Lawrence. mxd (ArcGIS Desktop) project file under the \ projects \ controls \ gulfofstlawrence folder. The relative path should be  "  ... \ Gulfofstlawrence \ gulf_of_st. _ Lawrence. mxd "  That is, string filename = @"  ... \ Gulfofstlawrence \ gulf_of_st. _ Lawrence. mxd  "  Experiences:  1  . Use relative paths to increase project portability. It makes a project easier during the transplantation process, saving a lot of time for arranging project-related documents. (If the absolute path is set ).  2  . Use the relative path to make the programCodeEasy to use  3 But one thing you must note: (you can only use the relative path in the same drive (for example, all in D ).
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.