文件流

一、文件操作常用的相关类(System.IO)

类名

作用

File

静态类,对文件整体操作、拷贝、删除、剪切等

Directory

静态类,操作目录(文件夹)

DirectoryInfo

文件夹的一个“类”,用来描述一个文件夹对象

FileInfo

文件类,用来描述一个文件对象

Path

对文件或目录的路径进行操作

Stream

文件流,抽象类,FileStream 文件流,MemoryStream 内存流,NetworkStream 网络流,StreamReader 快读读取文本文件,StreamWriter 快速写入文本文件。

二、Path

方法名

作用

ChangeExtension

修改文件的后缀,Path.ChangeExtension(@”c:\temp\f3.png”,”jpg”)

Combine

将两个路径合并成一个路径,Path.Combine(@”c:\temp”,”a.jpg”)

GetDiretoryName

得到文件的路径名,Path.GetDirectoryName(@”c:\temp\a.jpg”)

GetExtension

得到文件的扩展名,Path.GetExtension(@”c:\temp\a.jpg”)

GetFileName

得到文件路径的文件名部分

GetFileNameWithoutExtension

得到去除扩展名的文件名

GetFullPath

得到文件的全路径,可根据相对路径得到绝对路径

Assmbly.GetExecutiongssembly().Location

得到当前运行的程序集的路径

Demo1

 1 protected void Page_Load(object sender, EventArgs e)
 2         {
 3             string strPath = @"c:\1\erweima.jpg";
 4             Response.Write(Path.ChangeExtension(strPath, "png"));
 5             Response.Write("</br>");
 6             Response.Write(Path.Combine(@"c:\1", "1.jpg"));
 7             Response.Write("</br>");
 8             Response.Write(Path.GetDirectoryName(strPath));
 9             Response.Write("</br>");
10             Response.Write(Path.GetExtension(strPath));
11             Response.Write("</br>");
12             Response.Write(Path.GetFileName(strPath));
13             Response.Write("</br>");
14             Response.Write(Path.GetFileNameWithoutExtension(strPath));
15             Response.Write("</br>");
16             Response.Write(Path.GetFullPath(strPath));
17         }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-12
  • 2022-01-01
  • 2021-04-16
  • 2022-02-21
猜你喜欢
  • 2021-12-09
  • 2021-09-22
  • 2021-11-20
  • 2021-06-08
  • 2021-06-13
  • 2021-12-15
  • 2022-12-23
相关资源
相似解决方案