Export (0) Print
Expand All

Type.GetTypeFromProgID Method (String, String, Boolean)

.NET Framework 1.1

Gets the type associated with the specified program identifier (progID) from the specified server, specifying whether to throw an exception if an error occurs while loading the type.

[Visual Basic]
Overloads Public Shared Function GetTypeFromProgID( _
   ByVal progID As String, _
   ByVal server As String, _
   ByVal throwOnError As Boolean _
) As Type
[C#]
public static Type GetTypeFromProgID(
 string progID,
 string server,
 bool throwOnError
);
[C++]
public: static Type* GetTypeFromProgID(
 String* progID,
 String* server,
 bool throwOnError
);
[JScript]
public static function GetTypeFromProgID(
   progID : String,
 server : String,
 throwOnError : Boolean
) : Type;

Parameters

progID
The progID of the Type to get.
server
The server from which to load the type. If the server name is a null reference (Nothing in Visual Basic), this method automatically reverts to the local machine.
throwOnError
true to throw any exception that occurs.

-or-

false to ignore any exception that occurs.

Return Value

The Type associated with the specified program identifier (progID), if progID is a valid entry in the registry and a type is associated with it; otherwise, a null reference (Nothing in Visual Basic).

Exceptions

Exception Type Condition
ArgumentException progID is a null reference (Nothing in Visual Basic).
COMException The specified progID is not registered.

Remarks

This method is provided for COM support. Program IDs are not used in Microsoft .NET Framework because they have been superceded by the concept of namespace.

Example

[Visual Basic, C#, C++] The following example retrieves a type by passing a ProgID and server name. The example then displays the ClassID related to the ProgID, specifying whether to throw an exception if the ProgID or the server name is invalid.

[Visual Basic] 
Imports System
Class MainApp
    Public Shared Sub Main()
        Try
            ' Use Server localhost.
            Dim theServer As String = "localhost"
            ' Use  ProgID HKEY_CLASSES_ROOT\DirControl.DirList.1.
            Dim myString1 As String = "DirControl.DirList.1"
            ' Use a wrong ProgID WrongProgID.
            Dim myString2 As String = "WrongProgID"
            ' Make a call to the method to get the type information for the given ProgID.
            Dim myType1 As Type = Type.GetTypeFromProgID(myString1, theServer, True)
            Console.WriteLine("GUID for ProgID DirControl.DirList.1 is {0}.", myType1.GUID.ToString())
            ' Throw an exception because the ProgID is invalid and the throwOnError 
            ' parameter is set to True.
            Dim myType2 As Type = Type.GetTypeFromProgID(myString2, theServer, True)
        Catch e As Exception
            Console.WriteLine("An exception occurred. The ProgID is wrong.")
            Console.WriteLine("Source: {0}", e.Source.ToString())
            Console.WriteLine("Message: {0}", e.Message.ToString())
        End Try
    End Sub 'Main 
End Class 'MainApp

[C#] 

using System;
class MainApp 
{
    public static void Main()
    {
        try
        {
            // Use server localhost.
            string theServer="localhost";
            // Use  ProgID HKEY_CLASSES_ROOT\DirControl.DirList.1.
            string myString1 ="DirControl.DirList.1"; 
            // Use a wrong ProgID WrongProgID.
            string myString2 ="WrongProgID"; 
            // Make a call to the method to get the type information for the given ProgID.
            Type myType1 =Type.GetTypeFromProgID(myString1,theServer,true);
            Console.WriteLine("GUID for ProgID DirControl.DirList.1 is {0}.", myType1.GUID);
            // Throw an exception because the ProgID is invalid and the throwOnError 
            // parameter is set to True.
            Type myType2 =Type.GetTypeFromProgID(myString2, theServer, true);
        }
        catch(Exception e)
        {
            Console.WriteLine("An exception occurred. The ProgID is wrong.");
            Console.WriteLine("Source: {0}" , e.Source);
            Console.WriteLine("Message: {0}" , e.Message);
        }
    }
}

[C++] 

#using <mscorlib.dll>

using namespace System;

int main() {
   try {
      // Use server localhost.
      String* theServer=S"localhost";
      // Use  ProgID HKEY_CLASSES_ROOT\DirControl.DirList.1.
      String* myString1 =S"DirControl.DirList.1";
      // Use a wrong ProgID WrongProgID.
      String* myString2 =S"WrongProgID";
      // Make a call to the method to get the type information for the given ProgID.
      Type* myType1 =Type::GetTypeFromProgID(myString1, theServer, true);
      Console::WriteLine(S"GUID for ProgID DirControl.DirList.1 is {0}.",__box( myType1->GUID));
      // Throw an exception because the ProgID is invalid and the throwOnError
      // parameter is set to True.
      Type* myType2 =Type::GetTypeFromProgID(myString2, theServer, true);
   } catch (Exception* e) {
      Console::WriteLine(S"An exception occurred. The ProgID is wrong.");
      Console::WriteLine(S"Source: {0}" , e->Source);
      Console::WriteLine(S"Message: {0}" , e->Message);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Type Class | Type Members | System Namespace | Type.GetTypeFromProgID Overload List | GetProgIdForType | TypeLoadException | ReflectionPermission

Show:
© 2014 Microsoft