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