Detect password protected word file

后端 未结 1 463
北海茫月
北海茫月 2021-01-07 07:41

I am using \"netoffice\" library for extracting the text from word files. This should be automated process.

However, when the word file is password protected, the a

相关标签:
1条回答
  • 2021-01-07 07:57

    The following piece of code will help you skip password-protected files:

            int iFilesWithPassword = 0;
            Factory.Initialize();
            Application wordApplication = new NetOffice.WordApi.Application();
    
            try
            {
                // Attempt to open existing document. If document is not password protected then 
                // passwordDocument parameter is simply ignored. If document is password protected
                // then an error is thrown and caught by the catch clause the follows, unless 
                // password is equal to "#$nonsense@!"!                              
                Document newDocument = wordApplication.Documents.Open(@"C:\Users\Giorgos\Desktop\myNextFile.doc",
                                                                      confirmConversions: false,
                                                                      addToRecentFiles: false,
                                                                      readOnly: false,
                                                                      passwordDocument: "#$nonsense@!");
    
    
    
                // read text of document
                string text = newDocument.Content.Text;
            }
            catch(Exception e)
            {
                Exception inner = e.InnerException;
    
                if (inner != null && inner.InnerException != null)
                {
                    inner = inner.InnerException;
                    string sErrorMessage = inner.Message;
    
                    if (sErrorMessage.Contains("The password is incorrect."))
                    {
                        iFilesWithPassword++;
                    }
                }
    
            }
            finally
            {
                // close word and dispose reference 
                wordApplication.Quit();
                wordApplication.Dispose();
            }
    
    0 讨论(0)
提交回复
热议问题