C # Get relative path

Source: Internet
Author: User

App VS2010 created a WinForm project named RelativePathand placed on the desktop

Write the code to get the relative path in eight ways and output the display, running the effect as follows:

Here's a brief introduction to these eight ways to get a relative path:

1. Get and set the fully qualified path to the current directory (the directory from which the process starts)
string str1 = System.Environment.CurrentDirectory;    // result:c:xxxxxx

2. Get the current working directory of the application
string str2 = System.IO.Directory.GetCurrentDirectory ();    // result:c:xxxxxx

This is not necessarily the directory from which the program starts, it is possible that the program is placed in C:\xxx, this function may return C:\Documents and settings\wsy\, or C:\Program files\adobe\, sometimes not necessarily return something, which is The last directory that the program worked on , such as when you opened the E:\doc\my.doc file with Word, the method returned to E:\doc.

3. Get the path to the executable file that launched the application, not including the name of the executable file
string str3 = System.Windows.Forms.Application.StartupPath;    // result:c:xxxxxx

4. Get the path to the executable file that launched the application, including the name of the executable file
string str4 = System.Windows.Forms.Application.ExecutablePath;    // result:c:xxxxxxxxx. EXE

5. Gets the base directory of the current application domain for Thread, which is used by the Assembly resolver to probe assemblies
string STR5 = System.AppDomain.CurrentDomain.BaseDirectory; // result:c:xxxxxx

6. Get and set the name of the directory that contains the application
string str6 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;    // result:c:xxxxxx

7. Get the full path of the current process, including the file name
string this. GetType (). assembly.location;    // Result:c:xxxxxxxxx.exe

8. The full path of the Master module that gets the new process component and associates it with the currently active process, including the file name
string str8 = System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename;    // Result:c:xxxxxxxxx.vshost.exe

In addition, see Configuring Specific paths through XML files to achieve a reasonable location for planning configuration files, such as paths in configuration files in the Web

string @" ModuleM3ExampleMuColor.txt "   New StreamReader (path, System.Text.Encoding.Default); // Set Path

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.