首页 > 编程语言 >c#中的IO流(文件操作)

c#中的IO流(文件操作)

时间:2022-11-01 03:00:11浏览次数:38  
标签:文件 Console IO StreamWriter c# filePath sw WriteLine

获取磁盘信息DriveInfo

foreach (var div in DriveInfo.GetDrives())
{
    Console.WriteLine(div + "容量:"+ div.TotalSize);//获取到盘符
}

获取文件信息Environment

Console.WriteLine(Environment.CurrentDirectory);//获取当前目录

路径管理Path

string path = Path.Combine(Environment.CurrentDirectory, "net");//路径拼接,往后加一级文件夹,可以两个可以多个方法重载的。
Console.WriteLine(path);

目录管理:Dictionary文件夹创建删除移动等操作,DirectoryInfo获取文件夹信息

DirectoryInfo dinfo = Directory.CreateDirectory(path);//创建文件夹
Console.WriteLine(dinfo.Name);//获取文件夹名称

文件管理:File文件删除移动创建等操作,FileInfo获取文件信息

string filePath = path + "\\books.txt";//记录文件路径
FileStream f = File.Create(filePath);//创建文件(也会覆盖原文件),把路径给方法Create创建
f.Close();//因为Create是FileStream文件操作流对象,在操作流开启那么需要关闭后面的才能继续使用。否则进程无法释放。
FileInfo fi = new FileInfo(filePath);
Console.WriteLine(fi.CreationTime);//获取文件创建时间

文件操作:Stream派生出FileStream他在派生出StreamReader读取文件流和StreamWriter写入文件流

StreamWriter sw = new StreamWriter(filePath,true);//写入,构造方法重载,第二个参数是文件追加写入,不覆盖
try
{
    sw.WriteLine("写入一句话。");
}
catch (IOException ex)//IO流异常
{
    throw ex;//返回异常
}
finally 
{
    sw.Close();//关闭写入流
}
StreamReader sr = new StreamReader(filePath);//读取
string str;
while((str = sr.ReadLine()) !=null) Console.WriteLine(str);

StreamWriter流是非托管类型,net管不了,操作系统管理,需要手动关闭释放,否则一直占用进程资源。
using关键字大括号运行技术后可以自动释放。是官方给的,结合try--catch手动关闭的方式写的关键字,以后就不需要每次去手动关闭了。

using (StreamWriter sw = new StreamWriter(filePath, true))//using关键字:作用于对象在大概运行结束后自动close,自动释放资源。
{
    sw.WriteLine("写入一句话。");//节省了try-catch的代码
}

 

标签:文件,Console,IO,StreamWriter,c#,filePath,sw,WriteLine
From: https://www.cnblogs.com/longxinyv/p/16846479.html

相关文章

  • VSCode + Mingw64配置OpenGL环境
    前言最近在复习LearningOpenGL,以前学的时候是根据书的介绍直接使用VS2022来配置OpenGL的开发环境,根据书的操作使用Cmake构建glfw3,添加glad,然后正确设置项目以来并且正确......
  • SP15637 GNYR04H - Mr Youngs Picture Permutations
    SP15637GNYR04H-MrYoungsPicturePermutations-洛谷|计算机科学教育新生态(luogu.com.cn)好题。考虑从小到大(身高从高到低)安排每个数的位置。这样,已经被安排......
  • 008.从 XML 中构建 SqlSessionFactory
    1.在pom.xml文件中引入依赖<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</vers......
  • PCD_StateTypeDef
    /***@brief PCDStatestructuredefinition*/typedefenum{   HAL_PCD_STATE_RESET  =0x00,   HAL_PCD_STATE_READY  =0x01,   HAL_PCD_STATE_ERR......
  • 编译gRPC相关示例程序,undefined reference to `deflateInit2_'等相关错误解决
    编译gRPC相关示例程序时,出现如下链接错误:/home/suph/.local/lib/libgrpc.a(message_compress.cc.o):Infunction`zlib_compress(grpc_slice_buffer*,grpc_slice_buffer*......
  • Vue项目小功能:过度~transition
    Vue提供了transition的封装组件,来处理过渡以及动画使用过渡或动画的场景在下列情形中,可以给任何元素和组件添加进入/离开过渡条件渲染(使用v-if)条件展示(使用v-sh......
  • JavaScript中Array.from()方法的用法
    1.介绍作用:将一个伪数组对象或者可迭代的任意对象转换为一个真正的数组语法:Array.from(arrayLike[,mapFunction[,thisArg]])arrayLike:必传参数,指定需要转换为数......
  • [LeetCode] 1293. Shortest Path in a Grid with Obstacles Elimination
    Youaregivenan mxn integermatrix grid whereeachcelliseither 0 (empty)or 1 (obstacle).Youcanmoveup,down,left,orrightfromandtoanem......
  • USB_CfgTypeDef
    /** *@brief USBInitializationStructuredefinition */typedefstruct{ uint32_tdev_endpoints;          /*!<DeviceEndpointsnumber.   ......
  • Static方法
    方法的static修饰: 使用场景:对象表示自己的行为,访问实例成员的,该方法必须申明成实例方法;如果方法是以执行一个公用功能为目的,可以申请成静态方法。内存......