C#读取指定文件夹下所有图片路径,并将其写入TXT文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 练习1
{
class Program
{
static void Main(string[] args)
{
string[] path = Directory.GetFiles(@"D:/pictures");
for(int i=0;i<path.Length;i++)
{
Console.WriteLine(path[i]);
}
using (FileStream fsWrite = new FileStream(@"C:\Users\SSC\Desktop\00.txt", FileMode.OpenOrCreate, FileAccess.Write))
{
for (int i = 0; i < path.Length; i++)
{
string str = path[i]+"\n";//换行
byte[] buffer = System.Text.Encoding.Default.GetBytes(str);
fsWrite.Write(buffer, 0, buffer.Length);
}
}
Console.WriteLine("保存成功");
Console.ReadKey();
}
}
}
00.txt
D:/pictures\dave.png
D:/pictures\dtr_white_background.png
D:/pictures\opencv.png
D:/pictures\src.png
D:/pictures\timg.jpg