使用要加载的文档的完整路径和一个指示对该文档的文件访问权限的值,初始化 ServerDocument 类的新实例。
命名空间:  Microsoft.VisualStudio.Tools.Applications
程序集:  Microsoft.VisualStudio.Tools.Applications.ServerDocument(在 Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 中)
语法
声明
Public Sub New ( _
    documentPath As String, _
    access As FileAccess _
)
public ServerDocument(
    string documentPath,
    FileAccess access
)
参数
- documentPath
 类型:System.String
 要加载的文档的完整路径。
- access
 类型:System.IO.FileAccess
 一个指示对文档的文件访问权限的值。
异常
| 异常 | 条件 | 
|---|---|
| ArgumentNullException | documentPath 参数为 nullnull 引用(在 Visual Basic 中为 Nothing) 或为空,或者完全由空白字符组成。 | 
| ArgumentException | access 的值为 System.IO.FileAccess.Write。 | 
| FileNotFoundException | documentPath 指定的文件不存在。 | 
| DocumentNotCustomizedException | documentPath 参数指定的文件不具有自定义项,并且 access 的值为 System.IO.FileAccess.Read。 | 
| DocumentCustomizedWithPreviousRuntimeException | documentPath 指定的文件有不是用 Visual Studio 2010 Tools for Office Runtime 或 Microsoft Office 系统的 Visual Studio 工具创建的自定义项(3.0 版的运行时)。 | 
| UnknownCustomizationFileException | Visual Studio Tools for Office Runtime 不支持 documentPath 指定的文件的文件扩展名。 | 
备注
如果要以只读或只写访问权限打开磁盘中的文档,则使用此构造函数可以访问该文档中的缓存数据或部署清单信息。 默认情况下,其他 ServerDocument 构造函数以读/写访问权限打开文档。
示例
下面的代码示例使用 ServerDocument(String, FileAccess) 构造函数来创建一个以只读访问权限加载指定文档的新 ServerDocument。 然后,这段代码显示附加到文档的自定义项的部署清单的 URL。
此示例需要:
- 控制台应用程序项目或某些其他非 Office 项目。 
- 对下列程序集的引用: - Microsoft.VisualStudio.Tools.Applications.ServerDocument.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.dll(如果项目以 .NET Framework 4 为目标)。 - 或 
- Microsoft.VisualStudio.Tools.Applications.ServerDocument.v10.0.dll 和 Microsoft.VisualStudio.Tools.Applications.Runtime.v9.0.dll(如果该项目面向 .NET Framework 3.5)。 
 
- 位于代码文件顶部的用于 Microsoft.VisualStudio.Tools.Applications 和 Microsoft.VisualStudio.Tools.Applications.Runtime 命名空间的Imports(对于 Visual Basic)或 using(对于 C#)语句。 
Private Sub CreateServerDocumentReadOnly(ByVal documentPath As String)
    Dim runtimeVersion As Integer = 0
    Dim serverDocument1 As ServerDocument = Nothing
    Try
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath)
        If runtimeVersion = 3 Then
            serverDocument1 = New ServerDocument(documentPath, System.IO.FileAccess.Read)
            MessageBox.Show("The URL of the deployment manifest is: " & vbLf & _
                serverDocument1.DeploymentManifestUrl.ToString())
        End If
    Catch ex As System.IO.FileNotFoundException
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.")
    Catch ex As DocumentNotCustomizedException
        System.Windows.Forms.MessageBox.Show("The specified document does not " & _
            "have a customization.")
    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
    End Try
End Sub
private void CreateServerDocumentReadOnly(string documentPath)
{
    int runtimeVersion = 0;
    ServerDocument serverDocument1 = null;
    try
    {
        runtimeVersion = ServerDocument.GetCustomizationVersion(documentPath);
        if (runtimeVersion == 3)
        {
            serverDocument1 = new ServerDocument(documentPath,
                System.IO.FileAccess.Read);
            MessageBox.Show("The URL of the deployment manifest is: \n" +
                serverDocument1.DeploymentManifestUrl.ToString());
        }
    }
    catch (System.IO.FileNotFoundException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not exist.");
    }
    catch (DocumentNotCustomizedException)
    {
        System.Windows.Forms.MessageBox.Show("The specified document does not " +
            "have a customization.");
    }
    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();
    }
}
.NET Framework 安全性
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。