The Following Is In The .aspx Source File
Page Language="C#" AutoEventWireup="true" CodeBehind="TestForm.aspx.cs" Inherits="TestApp.TestForm"
!DOCTYPE html PUBLIC
Reference Page ="~/TestForm.aspx" // Note: Removed all HTML tags
protected void Upload_Click(object sender, EventArgs e)
{
String noPW = "C:\\Users\\David\\Desktop\\Doc1.docx";
String pwProtected = "C:\\Users\\David\\Desktop\\Test.docx";
// if (isProtected(pwProtected))
// outcome.Text = ("Document Is Password Protected");
// else
// outcome.Text = ("Document Is NOT Password Protected");
if (isProtected(noPW))
outcome.Text = ("Document Is Password Protected");
else
outcome.Text = ("Document Is NOT Password Protected");
}
The Following Is In The .aspx.cs Code Behind File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;
namespace TestApp
{
public partial class TestForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static bool isProtected(object filePath)
{
Application myapp = new Application();
object pw = "thispassword";
try
{
// Trying this for Word document
myapp.Documents.Open(ref filePath, PasswordDocument: ref pw); // try open
myapp.Documents[ref filePath].Close(); // close it if it does open
}
catch (COMException ex)
{
if (ex.HResult == -2146822880) // Can't Open Doc Caused By Invalid Password
return true;
else
Console.WriteLine(ex.Message + " " + ex.HResult); // For debugging, have only tested this one document.
}
return false;
}
}
}
At least on my computer, I get the expected output for both files, but this is not exactly what you call an exhaustive test of the code. In addition, I tried to upload a file using a FileUpload Control, and I got the COM error "Cannot Find C:\Windows\System\fileName.docx" which confused me just because the the file uploaded came from my desktop, but you probably know why that occurs as you are more familiar with ASP.NET than I am. Either way, this code is just something to try, hope that helps.