C # Summary of Paths (GO)

Source: Internet
Author: User

Source: http://www.cnblogs.com/yugongmengjiutian/articles/5521165.html

A period of time to write code often encountered the acquisition path problem, always feel a bit messy, so summed up the next, if you need to refer to

1. When Web development is in. Net

(1) ~/in the Runat=server control will automatically be resolved to the value of Request.applicationpath, is the current application directory such as

The ~/usercommunity/index.aspx corresponds to/henu. Rcenter.internal/usercommunity

(2)./indicates the current directory

(3).. /indicates a file in the previous level directory, such as the Usercommunity folder, can be: /module/to access the files in module

2 Get the path to the current request page: Request.filepath

3 Get the file path under the project:

String Path=appdomain. CurrentDomain. Setupinformation.applicationbase+ folder + files

If you get the path to the file under the Temp folder under Project

Can be used: string savepath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "temp" + @ "\" + filename

4 Server.MapPath Usage:

If you write the following path in the Userinfomanager.aspx code under the content folder under the project

This.temppath = Server.MapPath ("uploadresourceimage\\");

Then return to D:\wxm\ practice \content\uploadresourceimage

Other

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 includes 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 using the System.Environment.GetEnvironmentVariable () method can be easily obtained 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");

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 finally does not contain "\"; 3.c# Windows Service " AppDomain.CurrentDomain.BaseDirectory "or" System.Threading.Thread.GetDomain (). BaseDirectory "; with" Environment.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 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

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    //Note yourself two \    application.startuppath+ "\\3.jpg";

In C #, relative paths are used with "." and ".." Said, "." Represents the current directory, "..."    On behalf of the previous level. For example, if I use VS2005 to create a project named controls in the D:\My documents\visual Studio 2005\projects directory, there is a controls folder in the Projects folder,    The controls folder has three files: Controls.sln Controls folder Gulfofstlawrence folder. D:\My Documents\Visual Studio 2005\projects\controls\controls\bin\debug is the executable file that this simple project is capable of running Controls.exe now I want to D:\My Gulf_of_st._lawrence.mxd under the Documents\Visual Studio 2005\projects\controls\gulfofstlawrence folder (ArcGIS    Desktop) project file path. Then the relative path should be ". \.. \.. \gulfofstlawrence\gulf_of_st._lawrence.mxd "is string filename = @". \.. \.. \gulfofstlawrence\gulf_of_st._lawrence.mxd ";

Experience:

1. Using a relative path can increase the portability of the project. Make a project simple during the transplant, saving a lot of time to lay out the files associated with the project. (If the absolute path is set).

2. Using relative paths also makes program code simple

3. It is important to note, however, that the relative path can only be used in the same drive (e.g., all in D:).

C # Summary of Paths (GO)

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.