Night Walker
Night Walker

Reputation: 21260

Adding icons to application

I am trying to add to my application some icons via the code.

When i run my executable from debug / release compilation folder it works good .

But when I move my executable files to other folders it tells me that it doesn't find the icon files .

Any idea how/where I should tell the compiler to add those files to my final executable version ,that they will be visible .

my code looks like that

        Private Sub Set_Application_Icon()

        Dim Current As String
        Dim Parent As DirectoryInfo

        'current path is /bin/solution/
        Current = Directory.GetCurrentDirectory()
        Parent = Directory.GetParent(Current)
        ChDir(Parent.FullName)
        '/bin
        Current = Directory.GetCurrentDirectory()
        Parent = Directory.GetParent(Current)
        ChDir(Parent.FullName)
        'icons located at current directory now 


#If WizardVersion = 0 And ViewerVersion = 0 Then
        Me.Icon = New Icon(CurDir() & "\" & "LP_V2009c.ico")
#ElseIf WizardVersion = 0 And ViewerVersion = 1 Then
        Me.Icon = New Icon(CurDir() & "\" & "LP_V2009v.ico")
#Else
        Me.Icon = New Icon(CurDir() & "\" & "LP_V2009.ico")
#End If

    End Sub

thanks a lot for help

Upvotes: 0

Views: 1247

Answers (2)

brad.huffman
brad.huffman

Reputation: 1271

#If WizardVersion = 0 And ViewerVersion = 0 Then
        Me.Icon = New Icon(CurDir() & "\" & "LP_V2009c.ico")
#ElseIf WizardVersion = 0 And ViewerVersion = 1 Then
        Me.Icon = New Icon(CurDir() & "\" & "LP_V2009v.ico")
#Else
        Me.Icon = New Icon(CurDir() & "\" & "LP_V2009.ico")
#End If

First, those are compile time conditional statements. Not sure if you were wanting that evaluated at compile time, or run time. Second, CurDir is considered unreliable in determining application directory. In VS 2008, you can use the application's My namespace (My.Application.Info.DirectoryPath), or use Reflection to get the application's path. Last, as a previous answerer mentioned, the most reliable/portable way to do this would be to add the icons to your project as embedded resources. Hope that helps.

EDIT: The last link that I posted (embedded resources) shows how to do that. Here is a method from that article that retrieves an embedded icon from the assembly using Reflection.

Function GetEmbeddedIcon(ByVal strName As String) As Icon
    Return New Icon(System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(strName))
End Function

That should do it for you.

Upvotes: 1

Adam Maras
Adam Maras

Reputation: 26853

I suggest you look into adding icons as Resources, instead of referencing them as external files.

Upvotes: 2

Related Questions