C# 文件夹数据在更新怎么存入数组
时间: 2023-11-27 13:53:09 浏览: 49
你可以使用C#中的Directory类和FileInfo类来获取文件夹中的数据,并将它们存储在数组中。
以下是一个示例代码:
```csharp
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string folderPath = @"C:\Users\Username\Documents\MyFolder"; // 文件夹路径
string[] files = Directory.GetFiles(folderPath); // 获取文件夹中的所有文件
foreach (string file in files)
{
Console.WriteLine(file); // 输出文件路径
}
}
}
```
此代码将在控制台输出文件夹中的所有文件路径。你可以将它们存储在一个数组中,例如:
```csharp
string[] files = Directory.GetFiles(folderPath);
foreach (string file in files)
{
Console.WriteLine(file);
}
// 将文件路径存储在数组中
string[] fileArray = files;
```
这将在`fileArray`数组中存储文件夹中的所有文件路径。如果你想获取文件夹中的其他数据,例如子文件夹或文件夹属性,请查看Directory和FileInfo类的其他方法。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)