以下是在.aspx源文件
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");
}
以下是在.aspx.cs代码文件的背后
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;
}
}
}
至少在我的电脑上,我得到两个文件预期的输出,但是这不正是你所谓的代码的详尽测试。 此外,我试图上传使用FileUpload控件文件,我得到了COM错误“无法找到C:\ WINDOWS \ SYSTEM \ fileName.docx”,这让我感到困惑,只是因为上传的文件从我的桌面来了,但你大概知道为什么发生,你更熟悉ASP.NET比我。 无论哪种方式,这个代码只是一些尝试,希望帮助。