C#读取文件夹下所有文件
时间: 2023-05-15 13:06:36 浏览: 398
C是一种编程语言,由Dennis Ritchie在20世纪70年代开发。它是一种高级语言,可以用于开发各种应用程序,包括操作系统、游戏、数据库、网络应用程序等。C语言具有高效、可移植、灵活等特点,是计算机科学领域中最常用的编程语言之一。
相关问题
C#逐行读取文件夹内所有TXT文件
以下是C#逐行读取文件夹内所有TXT文件的示例代码:
```csharp
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string folderPath = @"C:\Temp"; // 文件夹路径
string[] fileEntries = Directory.GetFiles(folderPath, "*.txt"); // 获取所有 TXT 文件路径
foreach (string filePath in fileEntries)
{
Console.WriteLine($"Reading file: {filePath}");
using (StreamReader reader = new StreamReader(filePath))
{
string line;
while ((line = reader.ReadLine()) != null) // 逐行读取文本文件
{
Console.WriteLine(line);
}
}
}
}
}
```
以上代码会遍历文件夹中所有的 TXT 文件,并逐行读取每个文件的内容。您可以根据自己的需求对代码进行修改。
c# 读取文件夹里文件信息
你可以使用 C# 中的 `DirectoryInfo` 类来获取文件夹中的文件信息。以下是一个简单的示例代码,它遍历指定路径的文件夹,并输出每个文件的名称和大小:
```csharp
using System;
using System.IO;
class Program {
static void Main(string[] args) {
string path = @"C:\myfolder"; // 指定文件夹路径
DirectoryInfo dir = new DirectoryInfo(path);
// 遍历文件夹中的文件
foreach (FileInfo file in dir.GetFiles()) {
Console.WriteLine("File Name: {0}", file.Name);
Console.WriteLine("File Size: {0} bytes", file.Length);
}
}
}
```
你可以根据需求对代码进行修改,例如添加过滤条件、递归遍历子文件夹等。