简体   繁体   English

从文件夹读取所有文件

[英]Reading all files from an folder

So I have a folder named Documents. 因此,我有一个名为Documents的文件夹。 In this folder there are pdf files like for example 1.pdf 2.pdf 3.pdf 在此文件夹中有pdf文件,例如1.pdf 2.pdf 3.pdf

Now I want to read all those files from the folder and get specific information. 现在,我想从文件夹中读取所有这些文件并获取特定信息。 I need the 我需要

full filename: "1.pdf" 完整档案名称:“ 1.pdf”

extension: "pdf" 扩展名:“ pdf”

id: "1" id:“ 1”

modified date attribute: "10.10.2018" 修改日期属性:“ 10.10.2018”

Now my idea was to create a class something like: 现在,我的想法是创建一个类似以下内容的类:

public class FileElements
{
    string filename;
    string extension;
    string id;
    string modifiedDate;
}

Then I would create a list of the FileElements class. 然后,我将创建FileElements类的列表。

I guess then I have to get the path of the folder and go trough the files with a foreach method. 我想那我必须获取文件夹的路径,并使用foreach方法浏览文件。

Now my Problem is, I dont know how to go trough the folder and how I can get the specific information. 现在我的问题是,我不知道如何通过文件夹以及如何获取特定信息。

edit 编辑

It is not necessarily a .pdf file. 它不一定是.pdf文件。 It could be mixed up. 可能会混在一起。

using System.IO;

DirectoryInfo di = new DirectoryInfo(folder);
FileInfo[] files = di.GetFiles("*.pdf");

You should be able to get most of the information that you need from FileInfo , You will not need to use the custom object of FileElements contains everything you need, Or gives you a way to get it 您应该能够从FileInfo中获取所需的大多数信息,您将不需要使用FileElements的自定义对象,它包含所需的一切,或者为您提供了获取信息的方法

   List<FileElements> lstFileElements = new List<FileElements>;
   foreach(string pdfFile in Directory.GetFiles(folderPath, "*.pdf", SearchOption.AllDirectories)
   {
      FileElements temp = New FileElements();
      temp.filename = Path.GetFileName(pdfFile);
      temp.extension = Path.GetExtension(pdfFile);
      //etc...
      lstFileElements.Add(temp);
   }

Something like this? 像这样吗

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM