// Obtain the path or UNC location of the loaded file that contains the list.
Public static string sapplicationpath = assembly. getexecutingassembly (). location;
// Result: X: \ XXX. dll (directory where the. dll file is located +. dll file name)
// Obtain the complete path of the current process, including the file name (process name ).
String STR = This. GetType (). Assembly. location;
// Result: X: \ XXX \ xxx.exe (.exefile)
// Obtain the complete path of the new process component and associate it with the active process, including the file name (process name ).
String STR = system. Diagnostics. process. getcurrentprocess (). mainmodule. filename;
// Result: X: \ XXX \ xxx.exe (.exefile)
// Obtain and set the full path of the current directory (that is, the directory from which the process starts.
String STR = system. environment. currentdirectory;
// Result: X: \ XXX (directory of the .exe file)
// Obtain the base Directory of the current application domain of the current thread, which is used by the Assembly conflict resolution program to detect the assembly.
String STR = system. appdomain. currentdomain. basedirectory;
// Result: X: \ XXX \ (directory where the .exe file is located + "\")
// Obtain and set the name of the directory containing the application.
String STR = system. appdomain. currentdomain. setupinformation. applicationbase;
// Result: X: \ XXX \ (directory where the .exe file is located + "\")
// Obtain the path of the executable file that started the application, excluding the name of the executable file.
String STR = system. Windows. Forms. application. startuppath;
// Result: X: \ XXX (directory of the .exe file)
// Obtain the path of the executable file that started the application, including the name of the executable file.
String STR = system. Windows. Forms. application. executablepath;
// Result: X: \ XXX \ xxx.exe (.exefile)
// Obtain the current working directory of the application (unreliable ).
String STR = system. Io. Directory. getcurrentdirectory ();
// Result: X: \ XXX (directory of the .exe file)
The best way to retrieve paths in system services
String stmp = assembly. getexecutingassembly (). location;
Stmp = stmp. substring (0, stmp. lastindexof ('\'); // delete the file name
If (pathtype = 1)
Return stmp + @ "\ inputlog. xml ";
Else if (pathtype = 2)
Return stmp + @ "\ middledb. xml ";
Else
Return stmp + @ "\ appno. xml ";
Using system. IO;
String Path = "D: asdfasdf.bmp ";
String filename = path. getfilename (PATH); // file name
String ext = path. getextension (PATH); // Extension
1. determine whether a given path is valid and valid
Use the path. getinvalidpathchars or path. getinvalidfilenamechars method to obtain invalid path/file name characters. You can determine whether the path contains invalid characters;
2. How to determine whether a path string represents a directory or a file
The directory. exists or file. exist method is used. If the former is true, the path indicates the directory. If the former is true, the path indicates the file.
The disadvantage of the above method is that it cannot process non-existing files or directories. In this case, you can use the path. getfilename method to obtain the file name. If a path is not empty but the file name is empty, it indicates a directory; otherwise, it indicates a file;
3. Obtain a specific part of the path
Path. getdirectoryname: returns the directory information of the specified path string.
Path. getextension: returns the extension of the specified path string.
Path. getfilename: returns the name and extension of the specified path string.
Path. getfilenamewithoutextension: returns the name of the path string without the extension.
Path. getpathroot: Get the root directory information of the specified path.
4. accurately merge two paths without worrying about the annoying "\" Character
Using the path. Combine method, it will help you deal with annoying "\".
5. Obtain the path of the system directory
Environment. systemdirectory attribute: Get the fully qualified path of the system directory
Environment. getfolderpath method: the parameter type accepted by this method is environment. specialfolder enumeration. Through this method, you can obtain a large number of system folder paths, such as my computer, desktop, and system directory.
Path. gettemppath method: return the path of the temporary folder of the current system
6. Determine whether a path is an absolute or relative path.
Use path. ispathrooted
7. Read or set the current directory
Use the getcurrentdirectory and setcurrentdirectory methods of the Directory class
8. Use relative path
After setting the current directory (see the previous question), you can use the relative path. For a relative path, we can use path. getfullpath to obtain its fully qualified path (absolute path ).
Note: If you plan to use a relative path, we recommend that you set the working directory as the common starting point for each interaction file. Otherwise, some security risks that are difficult to detect may be introduced, attackers exploit this vulnerability to access system files.
9. Folder browsing dialog box (folderbrowserdialog class)
Main attribute: Description: The description text displayed on the Tree View control, for example, "Select Directory-exercise"; rootfolder: gets or sets the root folder from which to browse, for example, set in my computer (desktop by default); selectedpath: gets or sets the path selected by the user. If this attribute is set, the specified path is located when the dialog box is opened, the default value is the root folder. When the dialog box is closed, the path selected by the user is obtained based on this attribute. shownewfolderbutton: Get or set whether to display the new dialog box button;
Main method: showdialog: In this dialog box, the returned value is dialogresult type. If dialogresult. OK is returned, the user-selected path can be obtained through the selectedpath attribute;
========================================================== ==================================
1. system. Diagnostics. process. getcurrentprocess (). mainmodule. filename-obtain the complete path of the module.
2. system. environment. currentdirectory-Get and set the fully qualified directory of the current directory (the directory from which the process starts.
3. system. Io. Directory. getcurrentdirectory ()-Get the current working directory of the application. This is not necessarily the directory from which the program starts. It may be stored in c: \ XXX. This function may return c: \ Documents ents and Settings \ zyb \, or C: \ Program Files \ Adobe \, sometimes not necessarily returned.
4. system. appdomain. currentdomain. basedirectory-get the base Directory of the program.
5. system. appdomain. currentdomain. setupinformation. applicationbase-obtain and set the Directory Name of the application.
6. system. Windows. Forms. application. startuppath-obtain the executable file path of the application. The effect is the same as that of 2 and 5. Only 5 returns an extra "\" behind the string.
7. system. Windows. Forms. application. executablepath-obtain the path and file name of the executable file that started the application. The effect is the same as that of 1. For Windows and Web applications, the path they run is different, so the key is to determine which program is currently running. So we can use the following code:
String Path = "";
If (system. environment. currentdirectory = appdomain. currentdomain. basedirectory) // Windows applications are equal
... {Path = appdomain. currentdomain. basedirectory;
} Else... {Path = appdomain. currentdomain. basedirectory + "bin \";
} So if we write a class library, assembly is used in the class library. loadfrom, because it is a common class library, it may be used in Windows programs or web, so it is very convenient to use the above Code.
1. server. mappath
2. system. Windows. Forms. startuppath
3. method 2 of type. Assembly. location can be applied to console applications, winform applications, and Windows Services. method 1 can be applied to web applications and method 3 can be applied. But method 3 is the path to load the application. For a web application, the obtained path is: C: \ WINDOWS \ Microsoft. NET \ framework \ v1.1.4322 \ temporary ASP. NET Files directory. Therefore, we recommend that you use server. mappath for web projects. Otherwise, method 2 is recommended. If you create a new class library. You can add a reference to system. Windows. Forms. startuppath for use.