C#路径中获取文件全路径、目录、扩展名、文件名称

C#路径中获取文件全路径、目录、扩展名、文件名称

常用函数 需要引用System.IO   直接可以调用Path的静态方法

 

复制代码
 1     class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5 
 6             //获取当前运行程序的目录
 7             string fileDir = Environment.CurrentDirectory;
 8             Console.WriteLine("当前程序目录:"+fileDir);
 9             
10             //一个文件目录
11             string filePath = "C:\\JiYF\\BenXH\\BenXHCMS.xml";
12             Console.WriteLine("该文件的目录:"+filePath);
13 
14             string str = "获取文件的全路径:" + Path.GetFullPath(filePath);   //-->C:\JiYF\BenXH\BenXHCMS.xml
15             Console.WriteLine(str);
16             str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath); //-->C:\JiYF\BenXH
17             Console.WriteLine(str);
18             str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath);  //-->BenXHCMS.xml
19             Console.WriteLine(str);
20             str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS
21             Console.WriteLine(str);
22             str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath); //-->.xml
23             Console.WriteLine(str);
24             str = "获取路径的根目录:" + Path.GetPathRoot(filePath); //-->C:\
25             Console.WriteLine(str);
26             Console.ReadKey();
27 
28         }
29     }
复制代码

程序在桌面运行

 Path类介绍

复制代码
  1 #region 程序集 mscorlib.dll, v4.0.0.0
  2 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll
  3 #endregion
  4 
  5 using System;
  6 using System.Runtime.InteropServices;
  7 using System.Security;
  8 using System.Text;
  9 
 10 namespace System.IO
 11 {
 12     // 摘要: 
 13     //     对包含文件或目录路径信息的 System.String 实例执行操作。这些操作是以跨平台的方式执行的。
 14     [ComVisible(true)]
 15     public static class Path
 16     {
 17         // 摘要: 
 18         //     提供平台特定的替换字符,该替换字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。
 19         public static readonly char AltDirectorySeparatorChar;
 20         //
 21         // 摘要: 
 22         //     提供平台特定的字符,该字符用于在反映分层文件系统组织的路径字符串中分隔目录级别。
 23         public static readonly char DirectorySeparatorChar;
 24         //
 25         // 摘要: 
 26         //     提供平台特定的字符数组,这些字符不能在传递到 System.IO.Path 类的成员的路径字符串参数中指定。
 27         //
 28         // 返回结果: 
 29         //     当前平台的无效路径字符的字符数组。
 30         [Obsolete("Please use GetInvalidPathChars or GetInvalidFileNameChars instead.")]
 31         public static readonly char[] InvalidPathChars;
 32         //
 33         // 摘要: 
 34         //     用于在环境变量中分隔路径字符串的平台特定的分隔符。
 35         public static readonly char PathSeparator;
 36         //
 37         // 摘要: 
 38         //     提供平台特定的卷分隔符。
 39         public static readonly char VolumeSeparatorChar;
 40 
 41         // 摘要: 
 42         //     更改路径字符串的扩展名。
 43         //
 44         // 参数: 
 45         //   path:
 46         //     要修改的路径信息。该路径不能包含在 System.IO.Path.GetInvalidPathChars() 中定义的任何字符。
 47         //
 48         //   extension:
 49         //     新的扩展名(有或没有前导句点)。指定 null 以从 path 移除现有扩展名。
 50         //
 51         // 返回结果: 
 52         //     包含修改的路径信息的字符串。在基于 Windows 的桌面平台上,如果 path 是 null 或空字符串 (""),则返回的路径信息是未修改的。如果
 53         //     extension 是 null,则返回的字符串包含指定的路径,其扩展名已移除。如果 path 不具有扩展名,并且 extension 不是 null,则返回的路径字符串包含
 54         //     extension,它追加到 path 的结尾。
 55         //
 56         // 异常: 
 57         //   System.ArgumentException:
 58         //     path 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
 59         public static string ChangeExtension(string path, string extension);
 60         //
 61         // 摘要: 
 62         //     将字符串数组组合成一个路径。
 63         //
 64         // 参数: 
 65         //   paths:
 66         //     由路径的各部分构成的数组。
 67         //
 68         // 返回结果: 
 69         //     包含合并的路径的字符串。
 70         //
 71         // 异常: 
 72         //   System.ArgumentException:
 73         //     数组中的一个字符串包含 System.IO.Path.GetInvalidPathChars() 中定义的一个或多个无效字符。
 74         //
 75         //   System.ArgumentNullException:
 76         //     数组中的一个字符串为 null。
 77         public static string Combine(params string[] paths);
 78         //
 79         // 摘要: 
 80         //     将两个字符串组合成一个路径。
 81         //
 82         // 参数: 
 83         //   path1:
 84         //     要组合的第一个路径。
 85         //
 86         //   path2:
 87         //     要组合的第二个路径。
 88         //
 89         // 返回结果: 
 90         //     包含合并的路径的字符串。如果指定的路径之一是零长度字符串,则该方法返回其他路径。如果 path2 包含绝对路径,则该方法返回 path2。
 91         //
 92         // 异常: 
 93         //   System.ArgumentException:
 94         //     path1 或 path2 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
 95         //
 96         //   System.ArgumentNullException:
 97         //     path1 或 path2 为 null。
 98         public static string Combine(string path1, string path2);
 99         //
100         // 摘要: 
101         //     将三个字符串组合成一个路径。
102         //
103         // 参数: 
104         //   path1:
105         //     要组合的第一个路径。
106         //
107         //   path2:
108         //     要组合的第二个路径。
109         //
110         //   path3:
111         //     要组合的第三个路径。
112         //
113         // 返回结果: 
114         //     包含合并的路径的字符串。
115         //
116         // 异常: 
117         //   System.ArgumentException:
118         //     path1、path2 或 path3 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
119         //
120         //   System.ArgumentNullException:
121         //     path1、path2 或 path3 为 null。
122         public static string Combine(string path1, string path2, string path3);
123         //
124         // 摘要: 
125         //     将四个字符串组合成一个路径。
126         //
127         // 参数: 
128         //   path1:
129         //     要组合的第一个路径。
130         //
131         //   path2:
132         //     要组合的第二个路径。
133         //
134         //   path3:
135         //     要组合的第三个路径。
136         //
137         //   path4:
138         //     要组合的第四个路径。
139         //
140         // 返回结果: 
141         //     包含合并的路径的字符串。
142         //
143         // 异常: 
144         //   System.ArgumentException:
145         //     path1、path2、path3 或 path4 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
146         //
147         //   System.ArgumentNullException:
148         //     path1、path2、path3 或 path4 为 null。
149         public static string Combine(string path1, string path2, string path3, string path4);
150         //
151         // 摘要: 
152         //     返回指定路径字符串的目录信息。
153         //
154         // 参数: 
155         //   path:
156         //     文件或目录的路径。
157         //
158         // 返回结果: 
159         //     包含 path 目录信息的 System.String;或者为 null(如果 path 表示根目录、是空字符串 ("") 或是 null)。如果
160         //     path 没有包含目录信息,则返回 System.String.Empty。
161         //
162         // 异常: 
163         //   System.ArgumentException:
164         //     path 参数包含无效字符、为空、或仅包含空白。
165         //
166         //   System.IO.PathTooLongException:
167         //     path 参数的长度超过系统定义的最大长度。
168         [SecuritySafeCritical]
169         public static string GetDirectoryName(string path);
170         //
171         // 摘要: 
172         //     返回指定的路径字符串的扩展名。
173         //
174         // 参数: 
175         //   path:
176         //     从其获取扩展名的路径字符串。
177         //
178         // 返回结果: 
179         //     包含指定路径的扩展名(包括“.”)的 System.String、null 或 System.String.Empty。如果 path 为 null,则
180         //     GetExtension 返回 null。如果 path 不具有扩展名信息,则 GetExtension 返回 Empty。
181         //
182         // 异常: 
183         //   System.ArgumentException:
184         //     path 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
185         public static string GetExtension(string path);
186         //
187         // 摘要: 
188         //     返回指定路径字符串的文件名和扩展名。
189         //
190         // 参数: 
191         //   path:
192         //     从其获取文件名和扩展名的路径字符串。
193         //
194         // 返回结果: 
195         //     一个 System.String,由 path 中最后的目录字符后的字符组成。如果 path 的最后一个字符是目录或卷分隔符,则此方法返回 System.String.Empty。如果
196         //     path 为 null,则此方法返回 null。
197         //
198         // 异常: 
199         //   System.ArgumentException:
200         //     path 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
201         public static string GetFileName(string path);
202         //
203         // 摘要: 
204         //     返回不具有扩展名的指定路径字符串的文件名。
205         //
206         // 参数: 
207         //   path:
208         //     文件的路径。
209         //
210         // 返回结果: 
211         //     包含由 System.IO.Path.GetFileName(System.String) 返回的字符串的 System.String,但不包括最后的句点
212         //     (.) 和该句点后的所有字符。
213         //
214         // 异常: 
215         //   System.ArgumentException:
216         //     path 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
217         public static string GetFileNameWithoutExtension(string path);
218         //
219         // 摘要: 
220         //     返回指定路径字符串的绝对路径。
221         //
222         // 参数: 
223         //   path:
224         //     要为其获取绝对路径信息的文件或目录。
225         //
226         // 返回结果: 
227         //     包含 path 的完全限定位置的字符串,例如“C:\MyFile.txt”。
228         //
229         // 异常: 
230         //   System.ArgumentException:
231         //     path 是一个零长度字符串,仅包含空白或者包含 System.IO.Path.GetInvalidPathChars() 中已定义一个或多个无效字符。-
232         //     或 -系统未能检索绝对路径。
233         //
234         //   System.Security.SecurityException:
235         //     调用方没有所需的权限。
236         //
237         //   System.ArgumentNullException:
238         //     path 为 null。
239         //
240         //   System.NotSupportedException:
241         //     path 包含一个冒号(“:”),此冒号不是卷标识符(如,“c:\”)的一部分。
242         //
243         //   System.IO.PathTooLongException:
244         //     指定的路径、文件名或者两者都超出了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260
245         //     个字符。
246         [SecuritySafeCritical]
247         public static string GetFullPath(string path);
248         //
249         // 摘要: 
250         //     获取包含不允许在文件名中使用的字符的数组。
251         //
252         // 返回结果: 
253         //     包含不允许在文件名中使用的字符的数组。
254         public static char[] GetInvalidFileNameChars();
255         //
256         // 摘要: 
257         //     获取包含不允许在路径名中使用的字符的数组。
258         //
259         // 返回结果: 
260         //     包含不允许在路径名中使用的字符的数组。
261         public static char[] GetInvalidPathChars();
262         //
263         // 摘要: 
264         //     获取指定路径的根目录信息。
265         //
266         // 参数: 
267         //   path:
268         //     从其获取根目录信息的路径。
269         //
270         // 返回结果: 
271         //     一个包含 path 的根目录的字符串,例如“C:\”;如果 path 为 null,则为 null;如果 path 不包含根目录信息,则为空字符串。
272         //
273         // 异常: 
274         //   System.ArgumentException:
275         //     path 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。- 或 -System.String.Empty
276         //     被传递到 path。
277         [SecuritySafeCritical]
278         public static string GetPathRoot(string path);
279         //
280         // 摘要: 
281         //     返回随机文件夹名或文件名。
282         //
283         // 返回结果: 
284         //     随机文件夹名或文件名。
285         public static string GetRandomFileName();
286         //
287         // 摘要: 
288         //     创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。
289         //
290         // 返回结果: 
291         //     包含临时文件的完整路径的 System.String。
292         //
293         // 异常: 
294         //   System.IO.IOException:
295         //     发生 I/O 错误,例如没有提供唯一的临时文件名。- 或 -此方法无法创建临时文件。
296         [SecuritySafeCritical]
297         public static string GetTempFileName();
298         //
299         // 摘要: 
300         //     返回当前系统的临时文件夹的路径。
301         //
302         // 返回结果: 
303         //     包含临时目录的路径信息的 System.String。
304         //
305         // 异常: 
306         //   System.Security.SecurityException:
307         //     调用方没有所需的权限。
308         [SecuritySafeCritical]
309         public static string GetTempPath();
310         //
311         // 摘要: 
312         //     确定路径是否包括文件扩展名。
313         //
314         // 参数: 
315         //   path:
316         //     用于搜索扩展名的路径。
317         //
318         // 返回结果: 
319         //     如果路径中最后的目录分隔符(\\ 或 /)或卷分隔符 (:) 之后的字符包括句点 (.),并且后面跟有一个或多个字符,则为 true;否则为 false。
320         //
321         // 异常: 
322         //   System.ArgumentException:
323         //     path 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
324         public static bool HasExtension(string path);
325         //
326         // 摘要: 
327         //     获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。
328         //
329         // 参数: 
330         //   path:
331         //     要测试的路径。
332         //
333         // 返回结果: 
334         //     如果 path 包含绝对路径,则为 true;否则为 false。
335         //
336         // 异常: 
337         //   System.ArgumentException:
338         //     path 包含 System.IO.Path.GetInvalidPathChars() 中已定义的一个或多个无效字符。
339         public static bool IsPathRooted(string path);
340     }
341 }
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值