C# 讀寫文件摘要

主要參考地址:http://www.javashuo.com/article/p-rdeudvii-p.htmlhtml

 

首先下載微軟提供的工具:DsoFile  (微軟官網下載傳送門)git

 

讀寫自定義摘要信息(須要注意,自定義摘要信息只能添加一次,再添加會報錯,因此若是對應的name已經存在,只能採用修改的方式添加)ide

        /// <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
相關文章
相關標籤/搜索