WorkbookBase.HasPassword 属性
获取一个值,该值指示工作簿是否具有保护密码。
命名空间: Microsoft.Office.Tools.Excel
程序集: Microsoft.Office.Tools.Excel.v4.0.Utilities(在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)
语法
声明
Public ReadOnly Property HasPassword As Boolean
Get
public bool HasPassword { get; }
属性值
类型:System.Boolean
如果工作簿具有保护密码,则为 true;否则为 false。
示例
下面的代码示例检查 HasPassword 属性的值,以确定工作簿是否受密码保护。 如果工作簿不受密码保护,则此示例将 Password 属性设置为从用户输入获得密码,然后调用 SetPasswordEncryptionOptions 方法设置加密算法、密钥长度和加密提供程序的名称,并启用文件属性加密。 然后,此示例在工作表 Sheet1 中显示 PasswordEncryptionProvider、PasswordEncryptionAlgorithm、PasswordEncryptionKeyLength 和 PasswordEncryptionFileProperties 属性的值。 此示例假定存在一个名为 GetPasswordFromUserInput 的方法,该方法从用户输入获取一个强密码。
此示例针对的是文档级自定义项。
Private Sub WorkbookPasswordOptions()
If Not Me.HasPassword Then
Me.Password = GetPasswordFromUserInput()
Me.SetPasswordEncryptionOptions( _
"Microsoft RSA SChannel Cryptographic Provider", _
"RC4", 128, True)
End If
' Display the password properties in Sheet1.
Globals.Sheet1.Range("A1").Value2 = _
"Password Encryption Provider:"
Globals.Sheet1.Range("A2").Value2 = _
"Password Encryption Algorithm:"
Globals.Sheet1.Range("A3").Value2 = _
"Password Encryption Key Length:"
Globals.Sheet1.Range("A4").Value2 = _
"Password Encryption File Properties:"
Globals.Sheet1.Range("A1", "A4").Columns.AutoFit()
Globals.Sheet1.Range("B1").Value2 = _
Me.PasswordEncryptionProvider
Globals.Sheet1.Range("B2").Value2 = _
Me.PasswordEncryptionAlgorithm
Globals.Sheet1.Range("B3").Value2 = _
Me.PasswordEncryptionKeyLength
Globals.Sheet1.Range("B4").Value2 = _
Me.PasswordEncryptionFileProperties
Globals.Sheet1.Range("B1", "B4").Columns.AutoFit()
End Sub
private void WorkbookPasswordOptions()
{
if (!this.HasPassword)
{
this.Password = GetPasswordFromUserInput();
this.SetPasswordEncryptionOptions(
"Microsoft RSA SChannel Cryptographic Provider",
"RC4", 128, true);
}
// Display the password properties in Sheet1.
Globals.Sheet1.Range["A1", missing].Value2 =
"Password Encryption Provider:";
Globals.Sheet1.Range["A2", missing].Value2 =
"Password Encryption Algorithm:";
Globals.Sheet1.Range["A3", missing].Value2 =
"Password Encryption Key Length:";
Globals.Sheet1.Range["A4", missing].Value2 =
"Password Encryption File Properties:";
Globals.Sheet1.Range["A1", "A4"].Columns.AutoFit();
Globals.Sheet1.Range["B1", missing].Value2 =
this.PasswordEncryptionProvider;
Globals.Sheet1.Range["B2", missing].Value2 =
this.PasswordEncryptionAlgorithm;
Globals.Sheet1.Range["B3", missing].Value2 =
this.PasswordEncryptionKeyLength;
Globals.Sheet1.Range["B4", missing].Value2 =
this.PasswordEncryptionFileProperties;
Globals.Sheet1.Range["B1", "B4"].Columns.AutoFit();
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。