简体   繁体   English

c#检测powerpoint密码保护

[英]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#.我有一个问题,我想用 C# 检测 Powerpoint(仅限 ppt)文档是否受密码保护。 I can detect doc/docx/xls/xlsx/pptx now, but just can not detect ppt.我现在可以检测到 doc/docx/xls/xlsx/pptx,但就是检测不到 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:这是您可以适应的 VBA 中的一种方法:

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).这个想法是向 .Open 方法传递一个密码(在本例中为 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: @Viral @Alex 它也适用于 C#:

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.到目前为止,我还没有找到一个像样的 c# 解决方案来打开这样的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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