Compartir a través de


How to: Access Application Data

Applies to: InfoPath 2010 | InfoPath Forms Services | Office 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio Tools for Microsoft Office

The InfoPath managed code object model provides objects and collections that can be used to gain access to information about the InfoPath application, including information related to a form's underlying XML document and the form definition (.xsf) file. This data is accessed through the top-level object in the InfoPath object model hierarchy, which is instantiated by using the Application class.

In an InfoPath managed code form template project created using Microsoft Visual Studio Tools for Applications, you can use the this (C#) or Me (Visual Basic) keyword to access an instance of the Application class that represents the current InfoPath application, which can then be used to access the properties and methods of the Application class.

Example

Displaying the Application Name, Version, and Language ID

In the following example, the Name and Version properties of the Application class are used to return the name and version number of the running instance of InfoPath. The LanguageSettings property is then used to return a LanguageSettings object, which in turn is used to return the LCID (a four-digit number) for the language that is currently being used for the InfoPath user interface language. Finally, all of this information is displayed in a message box.

Important

For the LanguageSettings property to work, you must establish a reference to the Microsoft Office 14.0 Object Library from the COM tab of the Add Reference dialog box in Microsoft Visual Studio Tools for Applications. This will establish a reference to the Microsoft.Office.Core namespace, which contains the LanguageSettings class. Additionally, the form must be running as Full Trust.

This example requires a using or Imports directive for the Microsoft.Office.Core namespace in the declarations section of the form code module.

string appName = this.Application.Name;
string appVersion = this.Application.Version;
LanguageSettings langSettings = 
   (LanguageSettings)this.Application.LanguageSettings;
int langID = 
   langSettings.get_LanguageID(MsoAppLanaguageID.msoLanguageIDUI);

MessageBox.Show(
   "Name: " + appName + System.Environment.NewLine +
   "Version: " + appVersion + System.Environment.NewLine +
   "Language ID: " + langID);
Dim appName As String appName = Me.Application.Name
Dim appVersion As String = Me.Application.Version
Dim langSettings As LanguageSettings = _
   DirectCast(Me.Application.LanguageSettings, LanguageSettings)
Dim langID As Integer = _
   langSettings.LanguageID(MsoAppLanaguageID.msoLanguageIDUI)

MessageBox.Show( _
   "Name: " + appName + System.Environment.NewLine + _
   "Version: " + appVersion + System.Environment.NewLine + _
   "Language ID: " + langID)