C # obtain the path Summary

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 () gets 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 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: Obtain the base Directory of the program.

5. system. appdomain. currentdomain. setupinformation. applicationbase obtain and set 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 extra "\" behind the string.

7. system. Windows. Forms. application. executablepath obtains the executable file path and file name of the application. The effect is the same as that of 1.

Ii. Operating environment variables using system. environment. the getenvironmentvariable () method can easily obtain system environment variables, such as: system. environment. getenvironmentvariable ("WINDIR") can obtain the path of the Windows System directory.

The following are common values of environment variables:

System. environment. getenvironmentvariable ("WINDIR ");

System. environment. getenvironmentvariable ("include ");

System. environment. getenvironmentvariable ("tmp ");

System. environment. getenvironmentvariable ("Temp ");

System. environment. getenvironmentvariable ("path ");

Finally, I posted the variable values obtained through the above operations. In advance, I wrote a winform program. 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 winform

A: "application. startuppath": gets the path of the directory where the current application is located, and does not contain "\".

B: "application. executablepath": obtains 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: Get the path of the current application, excluding "\"; 3.c# use "appdomain" for Windows service. 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. 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: \ WINDOWS

System. environment. getenvironmentvariable ("include") = c: \ Program Files \ Microsoft Visual Studio. NET 2003 \ SDK \ V1.1 \ include \

System. environment. getenvironmentvariable ("tmp") = c: \ javase ~ 1 \ zhoufoxcn \ locals ~ 1 \ Temp

System. environment. getenvironmentvariable ("Temp") = c: \ police ~ 1 \ zhoufoxcn \ locals ~ 1 \ Temp

System. 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 \ binn

C # Relative Path System Path

// Obtain the path of the executable file that started the application, excluding the name of the executable file.

String str5 = application. startuppath; // you can obtain the file name of the currently executed exe.

String str1 = process. getcurrentprocess (). mainmodule. filename; // 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 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 starting 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; // you can get F: \ learning \ C # training \ win \ bin \ debug // Add two \ applications by yourself. startuppath + "\ 3.jpg ";

In C #, the relative path is represented by ".." and "..", ".." indicates the current directory, and ".." indicates the upper-level directory. For example, if vs2005 is used to create a project named controls in the D: \ My Documents ents \ Visual Studio 2005 \ projects directory, there is a controls folder in the projects folder, the controls folder contains three files: controls. the gulfofstlawrence folder in the SLN controls folder. D: \ My Documents ents \ 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 \ projects \ controls \ gulfofstlawrence. _ Lawrence. mxd (ArcGIS Desktop) project file path. The relative path should be ".. \.. \.. \ gulfofstlawrence \ gulf_of_st. _ Lawrence. mxd "is string filename = @".. \.. \.. \ gulfofstlawrence \ gulf_of_st. _ Lawrence. mxd ";

TIPS:

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. Using relative paths also simplifies the program code

3. Note: (only relative paths can be used in the same drive (for example, all in D ).

Turn: http://blog.sina.com.cn/s/blog_55dbebb00100dm4u.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.