C # Get the current path method collation

Source: Internet
Author: User

Https://www.cnblogs.com/tianma3798/p/6553863.html
1.

Gets the path or UNC location of the loaded file that contains the manifest. public static string Sapplicationpath = Assembly.getexecutingassembly (). Location;//result:x:\xxx\xxx\xxx.dll (the directory where the. dll file is located +.dll file name)

2.

Gets the full path of the current process, including the file name (process name). String str = this. GetType (). Assembly.location;//result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)

3.

Gets the full path of the new process component and its main module associated with the currently active process, including the file name (process name). String str = System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename;//result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)

4. Current working directory, that is, cmd > in front of the position

Gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts). string str = System.Environment.CurrentDirectory; Result:x:\xxx\xxx (the directory where the. exe file resides)
Gets the current working directory of the application (unreliable). String str = System.IO.Directory.GetCurrentDirectory ();//result:x:\xxx\xxx (the directory where the. exe file resides)

5.

Gets the base directory of the current application domain for Thread, which is used by the Assembly resolver to probe assemblies. String str = system.appdomain.currentdomain.basedirectory;//result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")

6.

Gets and sets the name of the directory that contains the application. String str = system.appdomain.currentdomain.setupinformation.applicationbase;//result:x:\xxx\xxx\ (the directory where the. exe file is located + "\" )

7.

Gets the path to the executable file that launched the application, not including the name of the executable file. String str = system.windows.forms.application.startuppath;//result:x:\xxx\xxx (the directory where the. exe file resides)
Gets the path to the executable file that launched the application, including the name of the executable file. String str = System.windows.forms.application.executablepath;//result:x:\xxx\xxx\xxx.exe (the directory where the. exe file resides +.exe file name)

This is the best way to take a path in a system service.

String stmp = assembly.getexecutingassembly (). Location;stmp = stmp. Substring (0, Stmp. LastIndexOf (' \ \ '));//delete 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, legal
An illegal path/file name character is obtained by Path.getinvalidpathchars or Path.getinvalidfilenamechars method, which can be used to determine if the path contains illegal characters;

2. How to determine whether a path string represents a directory or a file
Using the Directory.Exists or File.exist method, if the former is true, the path represents the directory, and if the latter is true, the path represents the file
One drawback of the above approach is that you cannot handle files or directories that do not exist. At this point you can consider using the Path.getfilename method to obtain the file name it contains, if a path is not empty, and the file name is empty then it represents the directory, otherwise represents the file;
3. Get a specific part of the path
Path.getdirectoryname: Returns directory information for the specified path string.
Path.getextension: Returns the extension of the specified path string.
Path.getfilename: Returns the file name and extension of the specified path string.
Path.getfilenamewithoutextension: Returns the file name of a path string that does not have an extension.
Path.getpathroot: Gets the root directory information for the specified path.
4. Merge two paths accurately without worrying about the annoying "\" character
Using the Path.Combine method, it will help you deal with annoying "\".
5. Get the path to the system directory
Environment.systemdirectory property: Gets the fully qualified path to the system directory
Environment.getfolderpath method: The method accepts the parameter type is Environment.SpecialFolder enumeration, through this method can obtain a large number of system folder path, such as My computer, desktop, system directory, etc.
Path.gettemppath method: Returns the path of the temporary folder for the current system
6, determine whether a path is an absolute path or a relative path
Using the Path.ispathrooted method
7. Read or set the current directory
Using the GetCurrentDirectory and SetCurrentDirectory methods of the Directory class
8. Using relative paths
After setting the current directory (see the previous question), you can use a relative path. For a relative path, we can use the Path.GetFullPath method to get its fully qualified path (absolute path).
Note: If you intend to use relative paths, it is recommended that you set the working directory as a common starting point for each interactive file, or you may introduce some hidden security vulnerabilities that can be exploited by malicious users to access system files.

9. Folder browse dialog box (FolderBrowserDialog Class)
Main properties: Description: The description text displayed on the TreeView control, such as "Select Directory-Practice" in, RootFolder: Gets or sets the root folder from which to start browsing, such as My Computer set in (default to Desktop)         ; SelectedPath: Gets or sets the path selected by the user, if this property is set, the dialog box is opened to the specified path, the default is the root folder, and when the dialog box is closed, the path selected by the user user is obtained; Shownewfolderbutton: Gets or sets whether the New dialog box button is displayed;
Main method: ShowDialog: Opens the dialog box, the return value is DialogResult type value, if DialogResult.OK, you can get the path selected by the user by the SelectedPath property;
=====================================================================

1. System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename-gets the full path of the module.

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, it is possible that the program is placed in the C:\xxx, and this function may return C:\Documents and settings\zyb\, or C:\Program files\adobe\, and sometimes not necessarily return anything.

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. For Windows programs 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 application is equal

... {path = AppDomain.CurrentDomain.BaseDirectory;

}else ... {Path = AppDomain.CurrentDomain.BaseDirectory + "bin\";

So if we write a class library, Assembly.LoadFrom is used in the class library, because it is a universal class library, it may be used in the Windows program may also use the web, then use the above code is very convenient.

1, Server.MapPath

2, System.Windows.Forms.StartupPath

3, Type.Assembly.Location Method 2 can be applied to the console application, WinForm applications, Windows Services, Method 1 can be applied to the Web application, Method 3 can be applied. But Method 3 is the path to load the application. In the case of a Web application, the path obtained is: C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Temporary the ASP. Net Files directory. So the Web project still uses Server.MapPath. Otherwise, use Method 2 is recommended. If you create a new class library yourself. Can be used after a reference to System.Windows.Forms.StartupPath is added.

C # Get the current path method collation

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.