C # obtain the file 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
Obtain and set the fully qualified directory of the current directory from which the process starts.
3. system. Io. Directory. getcurrentdirectory ()
Obtain 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 e with word: \ doc \ my.doc. After this method is executed, 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 name of the directory containing the application.
6. system. Windows. Forms. application. startuppath
Obtain the path of the executable file that started 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
Obtain the path and file name of the executable file that started the application. The effect is the same as that of 1.

 

For Windows and Web applications, the path they run is different, so the key is to determine which program is currently running. So we can use the following code:

String Path = ""; if (system. environment. currentdirectory = appdomain. currentdomain. basedirectory) // Windows applications are equal to {Path = appdomain. currentdomain. basedirectory;} else {Path = appdomain. currentdomain. basedirectory + "bin \";}

 

Ii. winform obtaining the file path

1. process. getcurrentprocess (). mainmodule. filename;
Obtain the file name of the currently executed exe.
2. environment. currentdirectory;
Obtain and set the fully qualified path of the current directory.
3. Directory. getcurrentdirectory ();
Obtain the current working directory of the application.
4. appdomain. currentdomain. basedirectory;
Gets the base Directory, which is used by the Assembly conflict resolution program to detect the Assembly and finally contains "\".
5. application. startuppath;
Obtain the path of the executable file that started the application, excluding the executable file name and "\".
6. application. executablepath;
Obtain the path of the executable file that started the application, including the name of the executable file.
7. appdomain. currentdomain. setupinformation. applicationbase;
Obtain or set the name of the directory containing the application.
8. system. Threading. thread. getdomain (). basedirectory"
Obtain the path of the directory where the current application is located. The path contains "\".
9. environment. currentdirectory
Obtain the path of the current application, excluding "\".
10. system. Io. Directory. getcurrentdirectory
Obtain the path of the current application, excluding "\".
11. system. Io. Directory. getfiles (PATH)
Returns the file name in the specified directory.
12. system. Io. Path. getfilenamewithoutextension (PATH );
Returns the name of a specified path string without an extension.

 

3. Obtain the root directory of the Web Application

1. httpruntime. appdomainapppath. tostring ()
Obtain the physical drive path of the application directory that hosts the application in the current application domain. Used for obtaining from app_data
2. server. mappath ("") or server. mappath ("~ /")
Returns the physical file path relative to the specified virtual path on the Web server.
3. Request. applicationpath. tostring ();
Obtain the virtual application root directory of ASP. NET applications on the server

Iv. Operating Environment Variables

The system. environment. getenvironmentvariable () method can be used to conveniently obtain system environment variables, such:
System. environment. getenvironmentvariable ("WINDIR") to 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 ");

C # obtain the file path

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.