Click here to Skip to main content
15,884,388 members
Articles / Programming Languages / Visual Basic
Alternative
Tip/Trick

Getting publishing information for a ClickOnce deployment

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
1 Apr 2012CPOL2 min read 14.7K   4  
This is an alternative for "Getting publishing information for a ClickOnce deployment"

Introduction

The original tip/trick was published using C#, so I decided to add a Visual Basic version. 

Sometimes it's good to know basic information concerning a ClickOnce deployment such as publish version, where the application was installed from, name of the manifest and so on.

This tip helps to resolve this information. It's usable only for applications that have ApplicationIdentity set (such as ClickOnce deployed applications). 

The code 

First, the full code:

VB.NET
Public Class PublishInfo
    ' Types of identities
    Public Enum IdentityType
        ' Deployment identity
        Deployment = 1
        ' Application identity
        Application = 2
    End Enum

    ' Regular expressions
    Private Shared _versionRegex As New System.Text.RegularExpressions.Regex("Version=(?<Major>\d*).(?<Minor>\d*).(?<Build>\d*).(?<Revision>\d*)", System.Text.RegularExpressions.RegexOptions.Compiled)
    Private Shared _cultureRegex As New System.Text.RegularExpressions.Regex(", Culture=(?<Culture>[^,]*),", System.Text.RegularExpressions.RegexOptions.Compiled)
    Private Shared _publicKeyTokenRegex As New System.Text.RegularExpressions.Regex(", PublicKeyToken=(?<PublicKeyToken>[^,]*),", System.Text.RegularExpressions.RegexOptions.Compiled)
    Private Shared _processorArchitectureRegex As New System.Text.RegularExpressions.Regex(", processorArchitecture=(?<ProcessorArchitecture>[^,]*)", System.Text.RegularExpressions.RegexOptions.Compiled)

    ' Main uri
    Private Shared _uri As System.Uri
    Public Shared Property Uri As System.Uri
        Get
            Return _uri
        End Get
        Private Set(value As System.Uri)
            _uri = value
        End Set
    End Property

    ' Deployment identity
    Private Shared _deploymentIdentity As Identity
    Public Shared Property DeploymentIdentity As Identity
        Get
            Return _deploymentIdentity
        End Get
        Private Set(value As Identity)
            _deploymentIdentity = value
        End Set
    End Property

    ' Application identity
    Private Shared _applicationIdentity As Identity
    Public Shared Property ApplicationIdentity As Identity
        Get
            Return _applicationIdentity
        End Get
        Private Set(value As Identity)
            _applicationIdentity = value
        End Set
    End Property

    ' Constructor for the PublishInfo class
    Shared Sub New()
        Dim identities As String
        Dim identity(2) As String

        Try
            ' Get the full name of the identity
            identities = System.AppDomain.CurrentDomain.ApplicationIdentity.FullName

            ' Parse uri
            PublishInfo.Uri = New System.Uri(identities.Substring(0, identities.IndexOf("#")))
            identities = identities.Substring(identities.IndexOf("#") + 1)

            ' Split the separate identities
            If (identities.IndexOf("\") > -1) Then
                identity = identities.Split("\")
            Else
                identity = identities.Split("/")
            End If

            ' Create the identity information
            PublishInfo.DeploymentIdentity = New Identity(identity(0), IdentityType.Deployment)
            PublishInfo.ApplicationIdentity = New Identity(identity(1), IdentityType.Application)
        Catch
        End Try
    End Sub

    ' Class that holds the identity information
    Public Class Identity

        ' Type of the identity
        Private _identityType As PublishInfo.IdentityType
        Public Property IdentityType As PublishInfo.IdentityType
            Get
                Return Me._identityType
            End Get
            Private Set(value As PublishInfo.IdentityType)
                Me._identityType = value
            End Set
        End Property

        ' Version information
        Private _version As System.Version
        Public Property Version As System.Version
            Get
                Return Me._version
            End Get
            Private Set(value As System.Version)
                Me._version = value
            End Set
        End Property

        ' Name of the application
        Private _applicationName As String
        Public Property ApplicationName As String
            Get
                Return Me._applicationName
            End Get
            Private Set(value As String)
                Me._applicationName = value
            End Set
        End Property

        ' Public key token
        Private _publicKeyToken As String
        Public Property PublicKeyToken As String
            Get
                Return Me._publicKeyToken
            End Get
            Private Set(value As String)
                Me._publicKeyToken = value
            End Set
        End Property

        ' Processor architecture
        Private _processorArchitecture As System.Reflection.ProcessorArchitecture
        Public Property ProcessorArchitecture As System.Reflection.ProcessorArchitecture
            Get
                Return Me._processorArchitecture
            End Get
            Private Set(value As System.Reflection.ProcessorArchitecture)
                Me._processorArchitecture = value
            End Set
        End Property

        ' Default constructor
        Private Sub New()
        End Sub

        ' Constructor
        Friend Sub New(identity As String, identityType As PublishInfo.IdentityType)
            Dim regexMatch As System.Text.RegularExpressions.Match
            Dim architecture As System.Reflection.ProcessorArchitecture

            Me.IdentityType = identityType
            Try
                ' Parse application name
                Me.ApplicationName = identity.Substring(0, identity.IndexOf(","))

                ' Parse version
                regexMatch = _versionRegex.Match(identity)
                Me.Version = New System.Version(Int16.Parse(regexMatch.Groups("Major").ToString()),
                                                 Int16.Parse(regexMatch.Groups("Minor").ToString()),
                                                 Int16.Parse(regexMatch.Groups("Build").ToString()),
                                                 Int16.Parse(regexMatch.Groups("Revision").ToString()))


                ' Parse public key token
                regexMatch = _publicKeyTokenRegex.Match(identity)
                Me.PublicKeyToken = regexMatch.Groups("PublicKeyToken").ToString()

                ' Parse processor architecture
                regexMatch = _processorArchitectureRegex.Match(identity)
                If (Not System.Enum.TryParse(Of System.Reflection.ProcessorArchitecture)(regexMatch.Groups("ProcessorArchitecture").ToString(), True, architecture)) Then
                    architecture = System.Reflection.ProcessorArchitecture.None
                End If
                Me.ProcessorArchitecture = architecture
            Catch
            End Try
        End Sub
    End Class
End Class 

Main parts explained 

The ApplicationIdentity of CurrentDomain returns a string representation for the application. This string contains three informative parts:

  1. Application URL
  2. # as a separator
  3. Deployment identity
  4. \ as a separator (1)
  5. Application identity

The PublishInfo constructor first separates the application URL and stores it statically in this class. After this the remainder of the string is splitted using the separator.

Even though the MSDN documentation states that \ is used as a separator, when I tested this / was the actual separator used. For this reason both variations are implemented in the code.

The Identity class holds the information for individual identity, application or deployment. In the constructor of this class the key information is parsed from the identity string and placed on proper properties such as Version.

The properties of the Identity class are:

  • IdentityType: Which type of identity is this: Application or Deployment
  • ApplicationName: either the application name or the deployment manifest name depending on the identity type
  • Version: Version for the identity described using System.Version class
  • PublicKeyToken: Public key token of the identity
  • ProcessorArchitecture: Processor architecture of the identity, described using System.Reflection.ProcessorArchitecture enumeration 

Using the class 

As an example of the usage of the class I wrote the property values of both identities to output window of Visual Studio using Debug class: 

VB.NET
System.Diagnostics.Debug.WriteLine("Uri: " + PublishInfo.Uri.ToString())
System.Diagnostics.Debug.WriteLine("")
System.Diagnostics.Debug.WriteLine("DeploymentIdentity")
System.Diagnostics.Debug.WriteLine("------------------")
System.Diagnostics.Debug.WriteLine("ApplicationName: " + PublishInfo.DeploymentIdentity.ApplicationName)
System.Diagnostics.Debug.WriteLine("Version: " + PublishInfo.DeploymentIdentity.Version.ToString())
System.Diagnostics.Debug.WriteLine("PublicKeyToken: " + PublishInfo.DeploymentIdentity.PublicKeyToken)
System.Diagnostics.Debug.WriteLine("ProcessorArchitecture: " + PublishInfo.DeploymentIdentity.ProcessorArchitecture.ToString())
System.Diagnostics.Debug.WriteLine("")
System.Diagnostics.Debug.WriteLine("ApplicationIdentity")
System.Diagnostics.Debug.WriteLine("-------------------")
System.Diagnostics.Debug.WriteLine("ApplicationName: " + PublishInfo.ApplicationIdentity.ApplicationName)
System.Diagnostics.Debug.WriteLine("Version: " + PublishInfo.ApplicationIdentity.Version.ToString())
System.Diagnostics.Debug.WriteLine("PublicKeyToken: " + PublishInfo.ApplicationIdentity.PublicKeyToken)
System.Diagnostics.Debug.WriteLine("ProcessorArchitecture: " + PublishInfo.ApplicationIdentity.ProcessorArchitecture.ToString()) 

The results were like 

Uri: file:///C:/.../publish/IdentityVB.application

DeploymentIdentity
------------------
ApplicationName: DomainIdentityVB.application
Version: 1.0.0.2
PublicKeyToken: 78d3234f402381cd
ProcessorArchitecture: X86

ApplicationIdentity
-------------------
ApplicationName: DomainIdentityVB.exe
Version: 1.0.0.2
PublicKeyToken: 78d3234f402381cd
ProcessorArchitecture: X86 

Note that if you're running the program from Visual Studio, you'll get different results, such as

Uri: http://tempuri.org/IdentityVB.application

DeploymentIdentity
------------------
ApplicationName: DomainIdentityVB.application
Version: 1.0.0.2
PublicKeyToken: 0000000000000000
ProcessorArchitecture: X86

ApplicationIdentity
-------------------
ApplicationName: DomainIdentityVB.exe
Version: 1.0.0.2
PublicKeyToken: 0000000000000000
ProcessorArchitecture: X86 

Also note that if the application hasn't been deployed yet, ApplicationIdentity is nothing so both identities in the shared helper class are null.

History

  • April 1, 2012: Alternative created. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Europe Europe
Biography provided

Comments and Discussions

 
-- There are no messages in this forum --