Wheels
Wheels

Reputation: 41

Retrieve 'Last Saved By' file property programmatically

I'm trying to access the 'Last Saved By' file property using C# as part of an MVC web app. I'm able to get pretty much every other property on the file from last modified date, to the owner and I've even used Shell32 to get really obscure properties.

However, I cannot find a way to get the 'Last Saved By' property when I am retrieving the audit properties for each file that I need to report on. The files I need to get this data from are all Excel.

Upvotes: 0

Views: 4014

Answers (3)

Glen sampson
Glen sampson

Reputation: 1

Here it is in Visual Basic

Dim sffile As Object = Microsoft.WindowsAPICodePack.Shell.ShellObject.FromParsingName(fileentry.FullName)
                    
Dim sf As Object = sffile.Properties.System.Document.LastAuthor.value

Upvotes: 0

Wheels
Wheels

Reputation: 41

The 'Last Saved By' property can be read using the WindowsAPICodePack.Shell libraries. This property is application specific, so it is not present on some files (for example it is available on .xls but not .csv). The 'Last Saved By' file property is named 'LastAuthor'.

I used nuget to get the package and the code below to access the property:

        string lastSavedBy = null;
        using (var so = ShellObject.FromParsingName(file))
        {
            var lastAuthorProperty =     so.Properties.GetProperty(SystemProperties.System.Document.LastAuthor);
            if (lastAuthorProperty != null)
            {
                var lastAuthor = lastAuthorProperty.ValueAsObject;
                if (lastAuthor != null)
                {
                    lastSavedBy = lastAuthor.ToString();
                }
            }
        }

Upvotes: 4

miroxlav
miroxlav

Reputation: 12194

You can access the property in

Workbook.BuiltinDocumentProperties(7)

Maybe it will have index 6 when accessed from the C#. See MSDN documentation.

Quick verification: in Immediate Window (Ctrl+G) of Excel VBA editor (Alt+F11) you can type
? ThisWorkbook.BuiltinDocumentProperties(7) and hit Enter to display the property. This is the Excel part.

There is also part how to call Excel from the C#, but I am not going to cover this one, you can find literally hundreds of answers and examples on this topic.

Maybe even more effective can be just adding reference to Microsoft.Office.Tools.Excel Namespace and working directly, without Excel.

Upvotes: 1

Related Questions