C# 读写文件摘要

主要参考地址:https://www.cnblogs.com/chenyangsocool/p/7511161.html

首先下载微软提供的工具:DsoFile  (微软官网下载传送门)

读写自定义摘要信息(需要注意,自定义摘要信息只能添加一次,再添加会报错,所以如果对应的name已经存在,只能采用修改的方式添加)

        /// <summary>
        /// 检测该文件属性中是否已经存在指定的自定义属性key
        /// </summary>
        /// <param name="file">本地的文件</param>
        /// <param name="key">自定义的key</param>
        /// <returns>存在key返回对应的值,不存在key返回string.empty</returns>
        private static string PropContains(string file, string key)
        {
            OleDocumentProperties odp = new OleDocumentProperties();
            odp.Open(file);

            try
            {
                //由于不能直接foreach,所以用了for循环
                for (int i = 0; i < odp.CustomProperties.Count; i++)
                {
                    if (odp.CustomProperties[i].Name == key)
                    {
                        return odp.CustomProperties[i].get_Value();
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.Error($"{file} 文件处理出错 ex:{ ex.ToString()}");
            }
            finally
            {
                odp.Close();
            }

            return string.Empty;
        }

        /// <summary>
        /// 修改自定义属性的属性值(存在则修改,不存在则添加)
        /// </summary>
        /// <param name="file">本地的文件</param>
        /// <param name="key">自定义的key</param>
        /// <returns>修改成功返回true,不成功返回false</returns>
        private static void PropChange(string file, string key, string value)
        {
            OleDocumentProperties odp = new OleDocumentProperties();
            odp.Open(file);

            try
            {
                //由于不能直接foreach,所以用了for循环
                for (int i = 0; i < odp.CustomProperties.Count; i++)
                {
                    if (odp.CustomProperties[i].Name == key)
                    {
                        //为指定自定义属性修改值
                        odp.CustomProperties[i].set_Value(value);
                        odp.Save();

                        return;
                    }
                }

                //不存在指定属性,则添加
                odp.CustomProperties.Add(key, value);
                odp.Save();
            }
            catch (Exception ex)
            {
                LogUtil.Error($"{file} 文件处理出错 ex:{ ex.ToString()}");
            }
            finally
            {
                odp.Close();
            }

        }
View Code

除开自定义摘要,还有很多自带的摘要信息可以直接使用,如下:

 [Guid("58968145-CF02-4341-995F-2EE093F6ABA3")]
    [TypeLibType(4288)]
    public interface SummaryProperties
    {
        [DispId(131073)]
        string Title { get; set; }
        [DispId(131074)]
        string Subject { get; set; }
        [DispId(131075)]
        string Author { get; set; }
        [DispId(131076)]
        string Keywords { get; set; }
        [DispId(131077)]
        string Comments { get; set; }
        [DispId(131078)]
        string Template { get; }
        [DispId(131079)]
        string LastSavedBy { get; set; }
        [DispId(131080)]
        string RevisionNumber { get; }
        [DispId(131081)]
        int TotalEditTime { get; }
        [DispId(131082)]
        dynamic DateLastPrinted { get; }
        [DispId(131083)]
        dynamic DateCreated { get; }
        [DispId(131084)]
        dynamic DateLastSaved { get; }
        [DispId(131085)]
        int PageCount { get; }
        [DispId(131086)]
        int WordCount { get; }
        [DispId(131087)]
        int CharacterCount { get; }
        [DispId(131088)]
        dynamic Thumbnail { get; }
        [DispId(131089)]
        string ApplicationName { get; }
        [DispId(131090)]
        int DocumentSecurity { get; }
        [DispId(131091)]
        string Category { get; set; }
        [DispId(131092)]
        string PresentationFormat { get; }
        [DispId(131093)]
        int ByteCount { get; }
        [DispId(131094)]
        int LineCount { get; }
        [DispId(131095)]
        int ParagraphCount { get; }
        [DispId(131096)]
        int SlideCount { get; }
        [DispId(131097)]
        int NoteCount { get; }
        [DispId(131098)]
        int HiddenSlideCount { get; }
        [DispId(131099)]
        int MultimediaClipCount { get; }
        [DispId(131100)]
        string Manager { get; set; }
        [DispId(131101)]
        string Company { get; set; }
        [DispId(131102)]
        int CharacterCountWithSpaces { get; }
        [DispId(131103)]
        bool SharedDocument { get; }
        [DispId(131104)]
        string Version { get; }
        [DispId(131105)]
        dynamic DigitalSignature { get; }
    }
View Code
作者:守望
QQ:497886344    微信: yellowgiutou
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/yellowgiutou/p/9989775.html