C# 截取带路径的文件名字,扩展名,等等 的几种方法

 C#对磁盘IO操作的时候,经常会用到这些,路径,文件,文件名字,文件扩展名.

    之前,经常用切割字符串来实现,

    可是经常会弄错. 尤其是启始位置,多少个字节,经常弄晕.

 

下面这种方法貌似比较简便:

 

string filePath = @"E:Randy0528中文目录JustTest.rar";
Response.Write("文件路径:"+filePath);
Response.Write("
更改路径字符串的扩展名。
");
Response.Write(System.IO.Path.ChangeExtension(filePath, "txt"));
Response.Write("
返回指定路径字符串的目录信息。。
");
Response.Write(System.IO.Path.GetDirectoryName(filePath));
Response.Write("
返回指定的路径字符串的扩展名。
");
Response.Write(System.IO.Path.GetExtension(filePath));
Response.Write("
返回指定路径字符串的文件名和扩展名。
");
Response.Write(System.IO.Path.GetFileName(filePath));
Response.Write("
返回不具有扩展名的指定路径字符串的文件名。
");
Response.Write(System.IO.Path.GetFileNameWithoutExtension(filePath));
Response.Write("
获取指定路径的根目录信息。
");
Response.Write(System.IO.Path.GetPathRoot(filePath));
Response.Write("
返回随机文件夹名或文件名。
");
Response.Write(System.IO.Path.GetRandomFileName());
Response.Write("
创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。
");
Response.Write(System.IO.Path.GetTempFileName());
Response.Write("
返回当前系统的临时文件夹的路径。
");
Response.Write(System.IO.Path.GetTempPath());
Response.Write("
确定路径是否包括文件扩展名。
");
Response.Write(System.IO.Path.HasExtension(filePath));
Response.Write("
获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。
");
Response.Write(System.IO.Path.IsPathRooted(filePath));
文件路径:E:Randy0528中文目录JustTest.rar
C 文件路径、文件名、扩展名 - skyroom - lantianroom更改路径字符串的扩展名。
C 文件路径、文件名、扩展名 - skyroom - lantianroomE:Randy0528中文目录JustTest.txt
C 文件路径、文件名、扩展名 - skyroom - lantianroom返回指定路径字符串的目录信息。。
C 文件路径、文件名、扩展名 - skyroom - lantianroomE:Randy0528中文目录
C 文件路径、文件名、扩展名 - skyroom - lantianroom返回指定的路径字符串的扩展名。
C 文件路径、文件名、扩展名 - skyroom - lantianroom.rar
C 文件路径、文件名、扩展名 - skyroom - lantianroom返回指定路径字符串的文件名和扩展名。
C 文件路径、文件名、扩展名 - skyroom - lantianroomJustTest.rar
C 文件路径、文件名、扩展名 - skyroom - lantianroom返回不具有扩展名的指定路径字符串的文件名。
C 文件路径、文件名、扩展名 - skyroom - lantianroomJustTest
C 文件路径、文件名、扩展名 - skyroom - lantianroom获取指定路径的根目录信息。
C 文件路径、文件名、扩展名 - skyroom - lantianroomE:
C 文件路径、文件名、扩展名 - skyroom - lantianroom返回随机文件夹名或文件名。
C 文件路径、文件名、扩展名 - skyroom - lantianroomct2h5b2h.sed
C 文件路径、文件名、扩展名 - skyroom - lantianroom创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。
C 文件路径、文件名、扩展名 - skyroom - lantianroomC:Documents and SettingsRandyLocal SettingsTemp	mpAD.tmp
C 文件路径、文件名、扩展名 - skyroom - lantianroom返回当前系统的临时文件夹的路径。
C 文件路径、文件名、扩展名 - skyroom - lantianroomC:Documents and SettingsRandyLocal SettingsTemp
C 文件路径、文件名、扩展名 - skyroom - lantianroom确定路径是否包括文件扩展名。
C 文件路径、文件名、扩展名 - skyroom - lantianroomTrue
C 文件路径、文件名、扩展名 - skyroom - lantianroom获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。
C 文件路径、文件名、扩展名 - skyroom - lantianroomTrue
原文地址:https://www.cnblogs.com/qq458978/p/4520185.html