1 //gets the path or UNC location of the loaded file that contains the manifest. 2 Public Static stringSapplicationpath =assembly.getexecutingassembly (). Location;3 4 //Result:x:\xxx\xxx\xxx.dll (the directory where the. dll file is located +.dll file name)5 6 7 8 //gets the full path of the current process, including the file name (process name). 9 Ten stringstr = This. GetType (). assembly.location; One A //result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename) - - the - //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). - - stringstr =System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename; + - //result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename) + A at - //gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts). - - stringstr =System.Environment.CurrentDirectory; - - //result:x:\xxx\xxx (the directory where the. exe file resides) in - to + //gets the base directory of the current application domain for Thread, which is used by the Assembly resolver to probe assemblies. - the stringstr =System.AppDomain.CurrentDomain.BaseDirectory; * $ //result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")Panax Notoginseng - the + //gets and sets the name of the directory that contains the application. A the stringstr =System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; + - //result:x:\xxx\xxx\ (the directory where the. exe file is located + "\") $ $ - - //gets the path to the executable file that launched the application, not including the name of the executable file. the - stringstr =System.Windows.Forms.Application.StartupPath;Wuyi the //result:x:\xxx\xxx (the directory where the. exe file resides) - Wu - About //gets the path to the executable file that launched the application, including the name of the executable file. $ - stringstr =System.Windows.Forms.Application.ExecutablePath; - - //result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename) A + the - //gets the current working directory of the application (unreliable). $ the stringstr =System.IO.Directory.GetCurrentDirectory (); the the //result:x:\xxx\xxx (the directory where the. exe file resides) the - in the This is the best way to take a path in a system service. the About stringSTMP =assembly.getexecutingassembly (). Location; the theStmp = stmp. Substring (0, Stmp. LastIndexOf ('\\') );//Delete file name the + if(PathType = =1 ) - the returnStmp +@"\inputlog.xml";Bayi the Else if(PathType = =2 ) the - returnStmp +@"\middledb.xml"; - the Else the the returnStmp +@"\appno.xml"; the - the the usingSystem.IO; the 94 stringPath ="d:asdfasdf.bmp"; the the stringFileName = Path.getfilename (Path);//file name the 98 stringext = path.getextension (Path);//Extended Name About - 101 102 1, determining whether a given path is valid, legal103Get an illegal path by Path.getinvalidpathchars or Path.getinvalidfilenamechars methodthe file name character, which can be used to determine whether the path contains illegal characters;104 the 2, how to determine whether a path string represents a directory or a file106 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 file107 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;108 3, get a specific part of the path109 path.getdirectoryname: Returns directory information for the specified path string. the path.getextension: Returns the extension of the specified path string. 111 path.getfilename: Returns the file name and extension of the specified path string. the path.getfilenamewithoutextension: Returns the file name of a path string that does not have an extension. 113 Path.getpathroot: Gets the root directory information for the specified path. the 4and accurately merge two paths without worrying about the annoying "\" character the using the Path.Combine method, it will help you deal with annoying "\". the 5, get the path to the system directory117 Environment.systemdirectory Property: Gets the fully qualified path to the system directory118 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. 119 Path.gettemppath Method: Returns the path of the temporary folder for the current system - 6, determine whether a path is an absolute or relative path121 using the Path.ispathrooted method122 7, read or set the current directory123 using the GetCurrentDirectory and SetCurrentDirectory methods of the directory class124 8, using relative paths the 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). 126 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. 127 - 9, folder browse dialog box (FolderBrowserDialog Class)129Main properties: Description: The description text displayed on the tree view control, as in the "Select Directory--practice "; RootFolder: Gets or sets the root folder from which to start browsing, such as My Computer set in (the default is the desktop); SelectedPath: Gets or sets the path selected by the user, if the property is set, the dialog box is located to the specified path, and the default is the root folder , which gets the path selected by the user based on this property when the dialog box is closed; Shownewfolderbutton: Gets or sets whether the New dialog box button is displayed; the 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;131===================================================================== the 133 1. System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename-gets the full path of the module. 134 135 2. System.environment.currentdirectory-gets and sets the fully qualified directory of the current directory (the directory from which the process starts). 136 137 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. 138 139 4. System.appdomain.currentdomain.basedirectory-gets the base directory of the program. $ 141 5. System.appdomain.currentdomain.setupinformation.applicationbase-gets and sets the name of the directory that includes the application. 142 143 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 returns a string after one more"\ "Only144 145 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 code146 147 stringPath ="";148 149 if(System.Environment.CurrentDirectory ==appdomain.currentdomain.basedirectory)//Windows applications are equal Max 151... {Path =AppDomain.CurrentDomain.BaseDirectory; the 153}Else... {Path = AppDomain.CurrentDomain.BaseDirectory +"bin\ ";154 155 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.156 157 1, Server.MapPath158 159 2, System.Windows.Forms.StartupPath the 161 3, Type.Assembly.Location Method 2 can be applied to console applications, WinForm applications, Windows Services, Method 1 can be applied to Web applications, and Method 3 is available. 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.
Go C # Read File path