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

超、凢脫俗 2022-05-25 02:09 362阅读 0赞

https://www.cnblogs.com/JiYF/p/6879139.html

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

复制代码

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

复制代码

程序在桌面运行

1070330-20170519163135713-1689365259.png

Path类介绍

复制代码

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

复制代码

发表评论

表情:
评论列表 (有 0 条评论,362人围观)

还没有评论,来说两句吧...

相关阅读