简体   繁体   中英

c# detect powerpoint password protected

guys. I have a problem that I want to detect a powerpoint(only ppt) document is password-protected or not with C#. I can detect doc/docx/xls/xlsx/pptx now, but just can not detect ppt. I searched on Google for a long time, but I did not find a more satisfactory answer. If you know how to solve this problem, please tell me.

Here's an approach in VBA that you can adapt:

Sub TestForPassword()

    Dim oPres As Presentation

    On Error Resume Next
    Set oPres = Presentations.Open("c:\temp\open.pptx::xopen::")
    If Not Err.Number = 0 Then
        MsgBox "Blimey, you trapped the error!" _
            & vbCrLf & Err.Number & vbCrLf & Err.Description
    End If

End Sub

The idea is to pass the .Open method a password (in this case xopen). If it's a password protected file and you pass it the correct password, the file opens. If it's password protected and you pass an incorrect password, you get an error. If the file's NOT password protected and you pass an incorrect password, the file still opens and there's no error.

幸运的是,我发现下面的代码能够打开受开放保护和受编辑保护的 ppt:

Presentation presentation = ppApp.Presentations.Open($"{presentationFile}::{password1}::", MsoTriState.msoFalse, MsoTriState.msoFalse, WithWindow: MsoTriState.msoFalse);

@Viral @Alex It works in C# as well:

Presentation presentation = ppApp.Presentations.Open($"{presentationFile}::{password}::", MsoTriState.msoFalse, MsoTriState.msoFalse, WithWindow: MsoTriState.msoFalse);

The real problem is when file is both open-protected and edit-protected. So far I haven't been able to find a decent c# solution to open such file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM