美文网首页
C# 文件操作

C# 文件操作

作者: 李霖弢 | 来源:发表于2019-10-29 17:28 被阅读0次

System.IO命名空间中,有以下类支持文件/路径操作

DriveInfo 驱动器信息

DriveInfo di = new DriveInfo(@"C:\");
静态方法
  • DriveInfo.GetDrives()
    获取该PC上所有驱动器实例的数组
DriveInfo[] allDrives = DriveInfo.GetDrives();
实例属性
  • Name 名称
  • TotalFreeSpace 剩余空间
  • VolumeLabel 别名/标签
  • RootDirectory
    根目录,为 DirectoryInfo 实例

DirectoryInfo 目录信息

实例方法
  • .GetFiles(string FileNameRule)
    • 传入文件名格式,获取该目录下所有符合条件的文件(FileInfo 实例数组)。
    • 入参如"*.*"即全部文件。
  • .GetDirectories(string DirectoryNameRule)
    • 传入目录名格式,获取该目录下所有符合条件的目录(DirectoryInfo 实例数组)。
    • 入参如"*.*"即全部目录。

FileInfo 文件信息

FileInfo fi = new FileInfo(@"C:\Users\62406\sampleQuotes.txt");

Directory 静态方法检索目录信息

目录没有直接的复制方法,只能通过新建目录并遍历复制所有文件的方式

静态方法
  • Directory.GetCurrentDirectory()
    获取当前工作目录的绝对路径字符串 (未SetCurrentDirectory时等于AppDomain.CurrentDomain.BaseDirectory)。
string currentDirUrl = Directory.GetCurrentDirectory();
//C:\Users\62406\Desktop\StudyC\ConsoleApp4\bin\Debug\netcoreapp2.1
  • Directory.SetCurrentDirectory(string DirectoryUrl)
    设置当前工作目录位置,此后各方法的相对路径都将相对于该新位置。
  • Directory.GetFiles(string TargetDirectoryUrl , string NameRule)
    Directory.GetDirectories(string TargetDirectoryUrl , string NameRule)
    Directory.GetFileSystemEntries(string TargetDirectoryUrl , string NameRule)
    获取目标目录下符合名称规则的所有文件/文件夹/文件和文件夹的绝对路径字符串。
string[] files = Directory.GetFiles(currentDirUrl , "*.txt");
  • Directory.Exists(string DirectoryUrl)
    判断目录是否存在
  • Directory.CreateDirectory(string DirectoryUrl)
    创建目录,若已存在则不处理
  • Directory.Move(string sourceUrl, string destUrl);
    剪切目录
  • Directory.Delete(string DirectoryUrl)
    删除目录

File 静态方法检索文件信息

静态方法
  • File.Exists(string FileUrl)
    判断文件是否存在
  • File.Create(string FileUrl)
    创建文件(若已存在则覆盖),返回值为FileStream实例
  • File.ReadAllBytes(string FileUrl)
    读取文件内容,返回 byte[]
  • File.Copy(string sourceUrl, string destUrl, bool overwrite);
    复制文件
  • File.Move(string sourceUrl, string destUrl);
    剪切文件
  • File.Delete(string FileUrl)
    删除文件
  • File.WriteAllLines(string sourceUrl,string[] content)
    创建/覆盖文件并写入多行文本
  • File.WriteAllText(string sourceUrl,string[] content)
    创建/覆盖文件并写入单行文本
  • File.ReadAllText(string sourceUrl)
    将文件内容作为单行文本输出
  • File.ReadAllLines(string sourceUrl)
    将文件内容作为string[]输出

Path 路径操作

静态方法
  • Path.Combine(string Name, string SubName)
    拼接下一级路径名
  • Path.GetRandomFileName()
    生成随机文件名
  • Path.GetFileName(string filePath)
    从文件完整路径中取出文件名
  • Path.GetFullPath(string relativePath)
    传入相对路径,获取绝对路径

FileStream 文件流

实例方法
  • .WriteByte(byte byteItem)
    写入内容
using (FileStream fs = System.IO.File.Create(filePath))
{
    string msg = "hello";
    byte[] myByte = System.Text.Encoding.UTF8.GetBytes(msg);
    foreach (var item in myByte)
    {
        fs.WriteByte(item);
    }
}
byte[] readBuffer = System.IO.File.ReadAllBytes(filePath);
string content = System.Text.Encoding.UTF8.GetString(readBuffer);
Console.WriteLine(content);//hello

StreamWriter

不同于FileStream输入byteStreamWriter会直接输入文本内容。
实例化时,第二个参数若为true,则保留原文件,否则直接新建一个覆盖。默认为false。

实例方法
  • .WriteLine(string textContent)
    在文件末尾添加一行文本。
using (System.IO.StreamWriter file = 
  new System.IO.StreamWriter(string sourceUrl, bool keepOriginal))
  {
    file.WriteLine(string textContent);
  }

StreamReader

读取后需要主动调用Close方法。

int counter = 0;  
string line;  
  
// Read the file and display it line by line.  
System.IO.StreamReader file =   
    new System.IO.StreamReader(@"c:\test.txt");  
while((line = file.ReadLine()) != null)  
{  
    System.Console.WriteLine(line);  
    counter++;  
}  
  
file.Close();  
System.Console.WriteLine("There were {0} lines.", counter);  
// Suspend the screen.  
System.Console.ReadLine();  

System.Diagnostics.Process

用于打开某个链接网址(弹窗)、文件目录,或系统特殊文件夹(控制面板等)

静态方法
  • Process.Start(string resourceUrl);
    直接打开文件或网址 (通过默认打开方式)
  • Process.Start (string appName, string resourceUrl)
    指定通过某种应用程序打开指定文件或网址
    • 浏览器 "chrome.exe","iexplore.exe"
    • 资源管理器 "explore.exe"**
实例方法
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "iexplore.exe";   //IE浏览器,可以更换
process.StartInfo.Arguments = "http://www.baidu.com";
process.Start();
System.Diagnostics.ProcessStartInfo processStartInfo = new System.Diagnostics.ProcessStartInfo();
processStartInfo.FileName = "explorer.exe";  //资源管理器
processStartInfo.Arguments = @"D:\";
System.Diagnostics.Process.Start(processStartInfo);

相关文章

  • C#文件操作

    C#中文件操作主要可以使用StreamReader和StreamWriter两个类

  • C# 文件操作

    System.IO命名空间中,有以下类支持文件/路径操作 DriveInfo 驱动器信息 静态方法 DriveIn...

  • C# 文件相关操作

    一、文件夹 1.选择文件夹 2.文件夹相关操作 C#文件夹相关操作主要使用Directory类和Directory...

  • lua简介

    C#与Lua的区别:(C#需要编译而lua不需要)C#只能在特定的操作系统中进行编译成dll文件,然后打包进安装包...

  • C#操作文件

    一、File类和FileInfo区别在于前者是静态类,后者需要实例化,实例化时需指定操作文件的路径,单一调用方法建...

  • C#操作文件

    读文件 检查文件大小 写文件 StreamWriter方式 FileStream方式 删除固定行的内容

  • C# csv文件操作

    学习地址:https://blog.csdn.net/xgf415/article/details/5136687...

  • Unity3D C#脚本获取指定目录所有指定类型文件并改名

    Tips:C#获取文件夹下的所有文件的文件名C#实现文件改名

  • C#操作FTP总结

    本文是对FTP相关操作,例如新建/删除目录、新建/删除文件、获取文件/目录列表等的总结,方便日后调用。 其实C#操...

  • C# EXCEL表的写入操作

    C#操作Excel表的操作实例

网友评论

      本文标题:C# 文件操作

      本文链接:https://www.haomeiwen.com/subject/cekgvctx.html