[C#]利用Dsofile 編輯office 檔案屬性內容

[C#]利用Dsofile 編輯office 檔案屬性內容

以前我個人是利用office automation來存取文件屬性,

當然使用automation 最怕就是記憶體無法釋放問題(檔案被鎖定),

且Server端需要安裝office 並設定一些細節,

如x64系統需新增desktop資料夾達到桌面互動、applaction pool權限設定...等,

所以我個人在使用office automation一定要繼承  IDisposable ,

client一律透過 using 包起來呼叫,

雖然大多數情況下都能順利釋放物件記憶體(但還是會有例外情況),

後來G一下發現原來Dsofile和SharpShell都能輕易達到我的需求,

簡單記錄一下使用 Dsofile。

 

1.下載並安裝 http://www.microsoft.com/en-us/download/details.aspx?id=8422

2.註冊 dsofile.dll  ( Regsvr32 C:\DsoFile\dsofile.dll )

3.專案加入 dsofile.dll 參考

image

 

讀取自訂屬性


private void LoadCustome()
        {
            List<TestModel> datas = new List<TestModel>();
            string srcoath = textBox1.Text.Trim();
            oleDocument = new OleDocumentProperties();
            try
            {
                oleDocument.Open(srcoath, false,
          dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
                foreach (CustomProperty cp in oleDocument.CustomProperties)
                {
                    if (cp.Type == dsoFilePropertyType.dsoPropertyTypeString)
                    {
                        TestModel data = new TestModel
                        {
                            Name = cp.Name,
                            Value = cp.get_Value()
                        };
                        datas.Add(data);
                    }
                }
                dataGridView1.DataSource = datas;
            }
            finally
            {
                oleDocument.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oleDocument);
            }        
        }

 

 

 

新增屬性


 private bool AddProps(string key,object value)
        {
            string srcoath = textBox1.Text.Trim();
            oleDocument = new OleDocumentProperties();
            try
            {
                oleDocument.Open(srcoath, false,
           dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
                oleDocument.CustomProperties.Add(key, ref  value);
                return true;
            }
            catch
            {
                return false;
                throw;
            }
            finally
            {
                //oleDocument.Save();
                oleDocument.Close(true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oleDocument);
                LoadCustome();
            }   
        }

 

 

 

刪除屬性


private bool DelProps(string key)
        {
            string srcoath = textBox1.Text.Trim();
            oleDocument = new OleDocumentProperties();
            try
            {
                oleDocument.Open(srcoath, false,
           dsoFileOpenOptions.dsoOptionOpenReadOnlyIfNoWriteAccess);
                foreach (CustomProperty cp in oleDocument.CustomProperties)
                {
                    if (cp.Type == dsoFilePropertyType.dsoPropertyTypeString)
                    {
                        if (cp.Name.ToLower().Trim() == key.ToLower().Trim())
                        {
                            cp.Remove();
                        }
                    }
                }     
               
                return true;
            }
            catch
            {
                return false;
                throw;
            }
            finally
            {
                //oleDocument.Save();
                oleDocument.Close(true);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(oleDocument);
                LoadCustome();
            }  
        
        }

 

 

 

 

 

參考

Dsofile.dll 檔案可讓您編輯時您並沒有安裝 Office 的 Office 文件內容

C# Easy Extension Properties

.NET Shell Extensions - Shell Property Sheets

Writing Windows Shell Extension with .NET 4 - Part 1

How to Write Windows Shell Extension with .NET Languages

Property Sheet Shell Extension in C#Add new metadata properties to a file