Stern Edi
Stern Edi

Reputation: 430

Application freezes when trying to read Author from .doc multiple files

I'm trying to make an app that displays all authors of .doc files from a Folder and its subfolders, the problem is that I used Directory.GetFiles("*.doc", SearchOption.AllDirectories) and when I'm trying to read from a folder with very much files, the app freeze in this point. Here is my code

FileInfo[] Files = dir.GetFiles("*.doc", SearchOption.AllDirectories);
foreach(FileInfo file in Files) 
{
    try
    {
        //ConvertDOCToDOCX(file.FullName);
        using(WordprocessingDocument sourcePresentationDocument = WordprocessingDocument.Open(file.FullName, false)) 
        {
            metadataList.Add(new Metadata() 
            {
                Name = "Title", Value = file.Name
            });
            metadataList.Add(new Metadata() 
            {
                Name = "Author", Value = sourcePresentationDocument.PackageProperties.Creator
            });
            metadataList.Add(new Metadata() 
            {
                Name = "", Value = ""
            });
        }
    }
}

Upvotes: 2

Views: 116

Answers (2)

Cadburry
Cadburry

Reputation: 1864

I think you dont need this "WordprocessingDocument" - this is producing a heavy load - you can just read the meta-information via .net default file methods.

For an example, you should take a look at Read/Write 'Extended' file properties (C#)

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1503200

the app freeze in this point

Yes, because you're doing a lot of work in the UI thread. Don't do that - never do that.

You should probably do all that work in a background thread (either with BackgroundWorker or maybe Task.Run) and then only modify the UI (on the UI thread) when you're done.

Upvotes: 0

Related Questions