FileStream类(文件流)
//FileStream fsRead = new FileStream(@"D:\C#ExampleFile\ex.txt", FileMode.OpenOrCreate, FileAccess.Read);
//byte[] buffer = new byte[1024 * 1024 * 5];
////返回本次实际读取到的有效字节数
//int r=fsRead.Read(buffer,0,buffer.Length); //返回有效字节数
////将字节数组中农的每个元素按照指定的编码格式解码成字符串
//string s = Encoding.Default.GetString(buffer,0,r);
////关闭文件流
//fsRead.Close();
////释放流所占用的资源
//fsRead.Dispose();
//Console.WriteLine(s);
//Console.ReadKey();
////===============将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源
using (FileStream fsWrite = new FileStream(@"D:\C#ExampleFile\ex.txt",FileMode.OpenOrCreate,FileAccess.Write))
{
string str = "写入文件测试示例";
byte[] buffer = Encoding.UTF8.GetBytes(str);
fsWrite.Write(buffer,0,buffer.Length);
}
Console.WriteLine("OK");
Console.ReadLine();
作业:循环连续读写大文件;
查找文件夹及文件是否存在;
==Directory静态类,操作文件夹
////File Path FileStream StreamReader StreamWriter
////创建文件夹
//Directory.CreateDirectory(@"d:\888\66\777");
//Console.Write("OK");
//Console.Read();
////删除文件夹
//Directory.Delete(@"d:\123",true);
//Console.Write("OK");
//Console.ReadKey();
//检测文件夹是否存在
//bool IsExist = Directory.Exists(@"D:\\111");
//if (IsExist == true)
//Console.Write("the directory is exsit!");
//else
//Console.Write("Not find the directoy");
//Console.Write(Convert.ToString(IsExist));
//Console.ReadKey();
////移动文件夹
//string SourceDir = "D:\\111";
//string TargetDir = "D:\\123\\234";
////Directory.Move(SourceDir, TargetDir); //不能跨盘符移动;TargetDir上一级目录123不存在时或已存在最后一级文件夹234时报异常
//CopyFolder(SourceDir, TargetDir);
//string[] path=Directory.GetFiles(@"d:\pic","*.jpg"); ////获得指定文件夹下所有文件的全路径
//string[] path=Directory.GetDirectories(@"d:\111"); ////获得指定目录下所有文件夹的全路径
//for (int i = 0; i < path.Length; i++)
//{
// Console.WriteLine(path[i]);
//}
//Console.ReadKey();
Application.StartupPath 获取程序启动路径
System.Environment.CurrentDirectory 获取或设置当前工作路径,比如程序打开了E:文件夹AAA,此时取值就是E:AAA,而不是程序exe所在路径。
本文介绍了C#中使用FileStream类进行文件读写的基本操作,包括循环读写大文件的方法。同时,文章提及了如何检查文件夹和文件是否存在,详细讨论了Directory静态类在文件夹管理中的应用。还提到了Application.StartupPath和System.Environment.CurrentDirectory属性,用于获取程序启动路径和当前工作路径。

2846

被折叠的 条评论
为什么被折叠?



