C # Get relative 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 with "Request.physicalapplicationpath Gets the physical path of the virtual directory where the site resides, and finally contains "\";
2.C # WinFormUse
A: "Application.startuppath": Gets the path to the directory where the current application resides, and finally does not contain" \ ";
B: "Application.executablepath": Gets the path to the current application file, including the name of the file;
C: "AppDomain.CurrentDomain.BaseDirectory ": Gets the path to 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 serviceWithAppDomain.CurrentDomain.BaseDirectoryOrSystem.Threading.Thread.GetDomain (). BaseDirectory”;
Withenvironment.currentdirectoryandSystem.IO.Directory.GetCurrentDirectory"Will get the path to the" system32 "directory;
If you want to use theApplication.startuppathOrApplication.executablepath", you need to manually add a reference to" System.Windows.Forms.dll "and declare the reference with" using System.Windows.Forms "at the beginning of the program;
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";

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 "
That 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:).


Ext.: http://blog.sina.com.cn/s/blog_55dbebb00100dm4u.html

C # Get relative 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.