views:

82

answers:

2

Hello

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

+2  A: 

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

Adam Maras
I added the files at the resources and i am trying to access to the icons like Me.Icon = New Icon("LP_V2009.ico") when i compile all ok , but when i run from diff location i get error with the path .When i have them in the Resources how i can access them ?
Night Walker
ok found the solution http://msdn.microsoft.com/en-us/library/ytt371w5%28VS.80%29.aspx
Night Walker
+1  A: 
#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.

brad.huffman
But how i can access the application files that i added ?is there some handle for that ?look what i wrote Adam Maras .
Night Walker
Check out the additional code. If you haven't checked out the articles/links I posted, definitely look at them. They should open up a few things.
brad.huffman
I added the icons to my resource of the program .How i can convert it to image ?New Icon(My.Resources.LP_V2009c)
Night Walker
Why do you need it as an image?
brad.huffman
Call the icon's ToBitmap method.
brad.huffman
Wow. So I didn't give you enough info to count as an answer. Ouch.
brad.huffman