C # Get Path

Source: Internet
Author: User

First, get the path of the current file
1. System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename
Gets the full 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, ah, it is possible that the program is placed in C:\www, this function may return C:\Documents and settings\zyb\, or C:\Program files\adobe\, sometimes not necessarily return something, This is the last directory that any application has ever operated on, such as when you open the E:\doc\my.doc file with Word, and you return to E:\doc when you execute this method.
4. System.AppDomain.CurrentDomain.BaseDirectory
Gets the base directory of the program.
5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
Gets and sets the name of the directory that contains the application.
6. System.Windows.Forms.Application.StartupPath
Gets the path to the executable file that launched the application. The effect is the same as 2, 5. Just 5 of the string returned is a "\" more.
7. System.Windows.Forms.Application.ExecutablePath
Gets the path and file name of the executable file that launched the application, with the same effect as 1.
Second, the operating environment variables
The System.Environment.GetEnvironmentVariable () method makes it easy to obtain system environment variables, such as:
System.Environment.GetEnvironmentVariable ("windir") Gets the path to the Windows system directory.
Here are some common environment variable values:
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/system32 Directory
Finally posted I do the above operation obtained the value of the variable, in advance, I wrote a WinForm program, the project file is stored in D:\Visual Studio projects\myapplication\lifeassistant, the compiled file is located 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. WebForm uses "Request.physicalapplicationpath to obtain the physical path of the virtual directory where the site resides, and finally contains" \ ";
2.c# for WinForm
A: "Application.startuppath": Gets the path of the directory where the current application resides, and finally does not contain "\";
B: "Application.executablepath": Gets the path of the current application file, including the name of the file;
C: "AppDomain.CurrentDomain.BaseDirectory": Gets the path of the directory where the current application resides, and finally contains "\";
D: "System.Threading.Thread.GetDomain (). BaseDirectory ": Gets the path to the directory where the current application resides, and finally contains" \ ";
E: "Environment.currentdirectory": Gets the path of the current application, and finally does not contain "\";
F: "System.IO.Directory.GetCurrentDirectory": Gets the path of the current application and does not include "\" at the end;
3.c# Windows service with "AppDomain.CurrentDomain.BaseDirectory" or "System.Threading.Thread.GetDomain (). BaseDirectory ";
Using "Environment.currentdirectory" and "System.IO.Directory.GetCurrentDirectory" will get the path of "system32" directory;
If you want to use "Application.startuppath" or "Application.executablepath", you need to manually add a reference to "System.Windows.Forms.dll", and at the beginning of the program use the "using System.Windows.Forms "declares the reference;
4. In the uninstall program get the directory of the system installation:
System.Reflection.Assembly Curpath = System.Reflection.Assembly.GetExecutingAssembly ();
String path=curpath.location;//get the path to the Installer class Setuplibrary file, get the directory where the file path is located, and get 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:\docume~1\zhoufoxcn\locals~1\temp
System.Environment.GetEnvironmentVariable ("TEMP") =c:\docume~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
2007-12-22 09:53
Gets the path to the executable file that launched the application, not including the name of the executable file.
String Str5=application.startuppath;
Gets the file name of the EXE that is currently executing.
String str1 =process.getcurrentprocess (). Mainmodule.filename;
Gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts). Note by definition, if the process is started in the root directory of a local or network drive, the value of this property is the drive name followed by a trailing backslash (such as "C: \"). If the process is started in a subdirectory, the value of this property is the drive and subdirectory path (such as "C:\mySubDirectory") without trailing backslashes.
String str2=environment.currentdirectory;
Gets the current working directory of the application.
String Str3=directory.getcurrentdirectory ();
Gets the base directory that is used by the Assembly resolver to probe assemblies.
String str4=appdomain.currentdomain.basedirectory;
Gets the path to the executable file that launched the application, not including the name of the executable file.
String Str5=application.startuppath;
Gets the path to the executable file that launched the application, including the name of the executable file.
String Str6=application.executablepath;
Gets or sets the name of the directory that contains the application.
String str7=appdomain.currentdomain.setupinformation.applicationbase;
Example
Application.startuppath;
Can get F:\learning\c#training\win\win\bin\debug
Pay attention to two of your own
application.startuppath+ "\\3.jpg";

C # Get Path

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.