更新:2007 年 11 月
获取加载到 ServerDocument 中的内存中文档的字节数组。
命名空间: Microsoft.VisualStudio.Tools.Applications
程序集: Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0(在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 中)
语法
声明
Public ReadOnly Property Document As Byte()
用法
Dim instance As ServerDocument
Dim value As Byte()
value = instance.Document
public byte[] Document { get; }
属性值
类型:array<System.Byte[]
加载到 ServerDocument 中的内存中文档的字节数组。
异常
异常 | 条件 |
---|---|
DocumentClosedException | 文档已关闭。 |
备注
如果 ServerDocument 是使用具有字节数组参数的 ServerDocument(array<Byte[], String) 构造函数创建的,或者是使用具有 Stream 参数的 ServerDocument(Stream, String) 构造函数创建的,则此属性返回已填充的字节数组。否则,此属性将返回空字节数组。
使用此属性可以对文档进行更改,并且可以在不将文档写入磁盘的情况下将文档发送到客户端。
示例
下面的代码示例使用 ServerDocument(array<Byte[], String) 构造函数,根据文件扩展名为 .xlsx 的 Excel 工作簿的字节数组创建新的 ServerDocument。然后该示例使用 Document 属性显示文档的字节数。
此示例需要在代码文件顶部使用对 Microsoft.VisualStudio.Tools.Applications.ServerDocument.v9.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll 程序集的引用,并对 Microsoft.VisualStudio.Tools.Applications 和 Microsoft.VisualStudio.Tools.Applications.Runtime 命名空间使用 Imports(对于 Visual Basic)或 using(对于 C#)语句。
Private Sub CreateServerDocumentFromByteArray(ByVal documentPath As String)
Dim runtimeVersion As Integer = 0
Dim serverDocument1 As ServerDocument = Nothing
Dim stream As System.IO.FileStream = Nothing
Try
runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
If runtimeVersion = 3 Then
' Read the file into a byte array.
stream = New System.IO.FileStream(documentPath, System.IO.FileMode.Open, _
System.IO.FileAccess.Read)
Dim buffer(Fix(stream.Length)) As Byte
stream.Read(buffer, 0, Fix(buffer.Length))
' Display the number of bytes in the document.
serverDocument1 = New ServerDocument(buffer, "*.xlsx")
MessageBox.Show("The Document property contains " & _
serverDocument1.Document.Length.ToString() & " bytes.")
End If
Catch ex As System.IO.FileNotFoundException
System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
Catch ex As UnknownCustomizationFileException
System.Windows.Forms.MessageBox.Show("The specified document has a file " & _
"extension that is not supported by Visual Studio Tools for Office.")
Finally
If Not (serverDocument1 Is Nothing) Then
serverDocument1.Close()
End If
If Not (stream Is Nothing) Then
stream.Close()
End If
End Try
End Sub
private void CreateServerDocumentFromByteArray(string documentPath)
{
int runtimeVersion = 0;
ServerDocument serverDocument1 = null;
System.IO.FileStream stream = null;
try
{
runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
if (runtimeVersion == 3)
{
// Read the file into a byte array.
stream = new System.IO.FileStream(
documentPath, System.IO.FileMode.Open,
System.IO.FileAccess.Read);
byte[] buffer = new byte[(int)stream.Length];
stream.Read(buffer, 0, (int)buffer.Length);
// Display the number of bytes in the document.
serverDocument1 = new ServerDocument(buffer,
"*.xlsx");
MessageBox.Show("The Document property contains " +
serverDocument1.Document.Length.ToString() +
" bytes.");
}
}
catch (System.IO.FileNotFoundException)
{
System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
}
catch (UnknownCustomizationFileException)
{
System.Windows.Forms.MessageBox.Show("The specified document has a file " +
"extension that is not supported by Visual Studio Tools for Office.");
}
finally
{
if (serverDocument1 != null)
serverDocument1.Close();
if (stream != null)
stream.Close();
}
}
权限
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。