How to: Load Document Properties
- 3 minutes to read
The Word Processing Document API allows you to load the document metadata (document properties) without loading the document itself. You can use the retrieved information to search for a specific document, catalogue files or collect statistics.
In this example, the information is used to sort documents by their modification date. Call the RichEditDocumentServerExtensions.LoadDocumentProperties method for each file in the folder. Check the Modified property and copy the file to the corresponding folder to sort it by tis modification date.
Important
The Rich
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System;
using System.IO;
using System.Diagnostics;
static void Main(string[] args)
{
DirectoryInfo directoryInfo =
new DirectoryInfo(@"C:\Users\Public\Documents\DevExpress Demos 24.2\Components\Data");
FileInfo[] files = directoryInfo.GetFiles("*.docx");
if (directoryInfo.Exists)
{
foreach (FileInfo file in files)
{
SortDocuments(file);
}
Process.Start("explorer.exe", @"C:\Documents");
}
}
static void SortDocuments(FileInfo file)
{
using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
{
//Load the metadata from the specified document:
ReadOnlyDocumentProperties docProperties =
wordProcessor.LoadDocumentProperties(File.OpenRead(file.FullName));
DateTime date = docProperties.BuiltIn.Modified;
//Check the year the document was last modified,
//And copy the file to the corresponding folder:
switch (date.Year)
{
case 2017:
string destFolder = "C://Documents//2017";
CreateDirectory(destFolder);
file.CopyTo(Path.Combine(destFolder, file.Name), true);
break;
case 2018:
destFolder = "C://Documents//2018";
CreateDirectory(destFolder);
file.CopyTo(Path.Combine(destFolder, file.Name), true);
break;
case 2019:
destFolder = "C://Documents//2019";
CreateDirectory(destFolder);
file.CopyTo(Path.Combine(destFolder, file.Name), true);
break;
case 2020:
destFolder = "C://Documents//2020";
CreateDirectory(destFolder);
file.CopyTo(Path.Combine(destFolder, file.Name), true);
break;
}
}
}
static void CreateDirectory(string path)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
}