C# 循环扫描文件夹 读取TXT文件 存储string
时间: 2023-09-10 13:05:18 浏览: 57
可以使用以下代码实现:
```csharp
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string folderPath = @"C:\folder\subfolder"; // 文件夹路径
string[] files = Directory.GetFiles(folderPath, "*.txt", SearchOption.AllDirectories); // 获取所有txt文件路径
foreach (string file in files)
{
string content = File.ReadAllText(file); // 读取文件内容
// 处理文件内容,存储到string中
}
}
}
```
其中`folderPath`为文件夹路径,`files`为获取的所有txt文件路径,通过`File.ReadAllText`读取每个文件的内容,可以将内容存储到`string`中进行后续处理。`SearchOption.AllDirectories`参数表示搜索子目录。
相关问题
c# 监控本地文件夹,读取TXT文件 多线程
可以使用C#的FileSystemWatcher类来监控本地文件夹,然后使用多线程来读取TXT文件的内容。以下是一个简单的代码示例,演示如何使用FileSystemWatcher和多线程来实现此目标:
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
class Program
{
static List<string> fileContents = new List<string>();
static void Main(string[] args)
{
string directoryPath = @"C:\example\";
FileSystemWatcher watcher = new FileSystemWatcher(directoryPath, "*.txt");
watcher.EnableRaisingEvents = true;
watcher.Created += OnCreated;
Console.WriteLine("Press 'q' to quit.");
while (Console.Read() != 'q') ;
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
Thread thread = new Thread(() =>
{
string[] lines = File.ReadAllLines(e.FullPath);
foreach (string line in lines)
{
fileContents.Add(line);
}
Console.WriteLine("File {0} has been read.", e.Name);
});
thread.Start();
}
}
```
在此示例中,我们首先创建了一个FileSystemWatcher对象来监控指定目录中的所有txt文件。然后,我们使用EnableRaisingEvents属性启用FileSystemWatcher对象的事件,然后订阅Created事件。当创建新的txt文件时,OnCreated方法将被调用,并在新的线程上读取txt文件的内容并将其存储在fileContents列表中。最后,我们打印一条消息,指示文件已被读取。
请注意,此示例中没有实现对fileContents列表的同步访问。如果多个线程同时访问列表,则可能会导致竞态条件和其他问题。要解决此问题,可以使用线程同步机制,如lock语句或Monitor类。
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 文件,并逐行读取每个文件的内容。您可以根据自己的需求对代码进行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)