c# 文件操作的一些函数
来源:互联网 发布:nginx和iis 编辑:程序博客网 时间:2024/06/06 19:13
- 遍历文件夹
public void GetAllFiles(string directoryPath){ var rootDirectory = new DirectoryInfo(directoryPath); foreach (var file in rootDirectory.GetFiles()) { Console.WriteLine("File Name: {0}", file.Name); } foreach (var directory in rootDirectory.GetDirectories()) { Console.WriteLine("Directory Name: {0}", directory.Name); this.GetAllFiles(directory.FullName); } }
- MemoryStream内存读写
public static void ShowMemoryString(string testString){ using (MemoryStream stream = new MemoryStream()) { if (stream.CanWrite) { byte[] buffer = Encoding.Default.GetBytes(testString); stream.Write(buffer, 0, buffer.Length); byte[] resultBuffer = stream.ToArray(); string resultString = Encoding.UTF8.GetString(resultBuffer); Console.WriteLine(resultString); } }}
- 二进制流文件读取
第一种方法(直接申请等于数据长的缓存)
public static void ShowFileContent(string filePath) { FileStream fileStream = File.Open(filePath, FileMode.Open); byte[] fileContentBuffer = new byte[fileStream.Length]; fileStream.Read(fileContentBuffer, 0, fileContentBuffer.Length); fileStream.Close(); string fileContent = Encoding.Default.GetString(fileContentBuffer); Console.WriteLine(fileContent);}
第二种方法(第一种的改进 使用完流之后进行关闭)
public static void ShowFileContent(string filePath) { // Way 2 byte[] fileContentBuffer; using (FileStream fileStream = File.Open(filePath, FileMode.OpenOrCreate)) { fileContentBuffer = new byte[fileStream.Length]; fileStream.Read(fileContentBuffer, 0, fileContentBuffer.Length); } string fileContent = Encoding.Default.GetString(fileContentBuffer); Console.WriteLine(fileContent);}
第三种方法 (每次读四字节)
public static void CopyFileContent(string sourceFilePath, string destinationFilePath) { using (FileStream destinationFileStream = File.Open( destinationFilePath, FileMode.Create, FileAccess.Write)) { // Way 1 const int bufferSize = 4 * 1024 * 1024; byte[] fileContentBuffer = new byte[bufferSize]; using (FileStream sourceFileStream = File.Open( sourceFilePath, FileMode.Open, FileAccess.Read)) { int data; do { data = sourceFileStream.Read(fileContentBuffer, 0, fileContentBuffer.Length); destinationFileStream.Write(fileContentBuffer, 0, data); } while (data > 0); } }}
第四种 更简化
public static void ShowFileContent(string filePath) { // Way 3 using (StreamReader fileReader = new StreamReader( File.Open(filePath, FileMode.OpenOrCreate))) { Console.WriteLine(fileReader.ReadToEnd()); }}
0 0
- c# 文件操作的一些函数
- 文件操作的一些函数
- 文件操作的一些函数
- 文件操作的一些函数
- c# 操作oracle的一些通用函数
- C#操作word的一些函数
- C#对字符串的一些操作函数
- C#文件的一些操作,值得看看!
- C#文件的一些操作,值得看看!
- C#中一些文件的操作
- 文件操作函数的一些问题
- linux下的一些文件操作函数
- linux文件操作的一些函数
- 常用的一些文件操作函数
- C# 操作INI文件的函数 INIClass
- c#中文件操作的一些相关函数
- C#中对XML文件的一些基本操作
- C# XML文件的一些操作(XmlDocument、XmlElement、XmlNode)
- 你在无人的街道,背影苍凉
- Exception in thread "http-8080-3" java.lang.OutOfMemoryError: PermGen space错误解决办法
- Redis的三种启动方式
- github常见操作和常见错误!错误提示:fatal: remote origin already exists.
- 活动的启动模式(笔记)
- c# 文件操作的一些函数
- 谈谈Processing 3D世界 六
- poj 3071 Football 概率动态规划
- pwd--print name of current/working directory
- CentOS下python2.6升级为python2.7
- 2016年Spring 官网下载地址
- Laravel 5 中防止 XSS 跨站攻击的例子
- 近 100 个 Linux 常用命令大全
- irq分析之irqchip.c