从文件夹中获取文件 [英] Fetch files from folder

查看:76
本文介绍了从文件夹中获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的文件夹中有一些文件.我想从该文件夹中获取文件,并将每个文件转换为二进制流的对象并存储在集合中.从集合中,我想检索每个二进制流对象.

I have somes files in a folder. I want to fetch the files from that folder and convert each file to an object of binary stream and store in a collection. And from the collection, I want to retrieve each binary stream objects. How is it possible using ASP.Net with c# ?

推荐答案

如果您希望将其存储在MemoryStream中,则可以尝试

If you wish to have it stored in a MemoryStream you could try

List<MemoryStream> list = new List<MemoryStream>();
string[] fileNames = Directory.GetFiles("Path");
for (int iFile = 0; iFile < fileNames.Length; iFile++)
{
    using (FileStream fs = new FileStream(fileNames[iFile], FileMode.Open))
    {
        byte[] b = new byte[fs.Length];
        fs.Read(b, 0, (int)fs.Length);
        list.Add(new MemoryStream(b));
    }
}

如果您希望将文件名保留为键,甚至可以使用词典

Or even use a Dictionary if you wish to keep the file names as keys

Dictionary<string, MemoryStream> files = new Dictionary<string, MemoryStream>();
string[] fileNames = Directory.GetFiles("Path");
for (int iFile = 0; iFile < fileNames.Length; iFile++)
{
    using (FileStream fs = new FileStream(fileNames[iFile], FileMode.Open))
    {
        byte[] b = new byte[fs.Length];
        fs.Read(b, 0, (int)fs.Length);
        files.Add(Path.GetFileName(fileNames[iFile]), new MemoryStream(b));
    }
}

这篇关于从文件夹中获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆