axbeit
axbeit

Reputation: 883

Reading all files from an folder

So I have a folder named Documents. In this folder there are pdf files like for example 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"

extension: "pdf"

id: "1"

modified date attribute: "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.

I guess then I have to get the path of the folder and go trough the files with a foreach method.

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. It could be mixed up.

Upvotes: 1

Views: 4071

Answers (2)

Cal-cium
Cal-cium

Reputation: 654

   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?

Upvotes: 0

Lord Darth Vader
Lord Darth Vader

Reputation: 2005

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

Upvotes: 1

Related Questions