Select to view content in your preferred language

How do I access my LayerFiles in my Addin?

2662
7
04-12-2011 06:23 AM
by Anonymous User
Not applicable
Original User: Hornbydd

All,

I've been developing my first ever ArcGIS 10 Addin. If I have understood the help correctly I can embed files into my addin, I have done this with 3 LayerFiles. See image below titled folderstructure, I have also set the Copy to Output Directory to Copy always.

I use the following bit of code to get a handle on the path:

Dim s As String
Dim path As String
s = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
path = System.IO.Path.GetDirectoryName(s) & "\Layer Files\"


So I build the project in VS2008 and then copy JUST the esriAddIn file to c:\temp.
When I run the tool to try and get a handle on the LayerFile inside the esriAddIn file I get the error message as shown by the image title errormessage. Note this has a very different path.

Have I misunderstood what an embedded file can be? If not how do you drill down to the LayerFile as ultimately I want to get hold of the symbology store inside it with code similar to below:

s = path & "Polygon.lyr"
pLayerFile = New LayerFile
pLayerFile.Open(s)
pLayer2 = pLayerFile.Layer
 pGeoFeatureLayer2 = pLayer2
pGeoFeatureLayer.Renderer = pGeoFeatureLayer2.Renderer
pLayerFile.Close()


Duncan
0 Kudos
7 Replies
by Anonymous User
Not applicable
Original User: jeffhamblin

Hi Duncan,

The Add-In code actually runs from the AssemblyCache location. Everything in the zipped ".esriaddin" file is extracted there for execution. The path shown in the error dialog looks incomplete -- it doesn't have the usual GUID folder for the Add-In. I suspect it might be because you are using "GetExecutingAssembly.Location" to get the path.

Try this method of getting the path (from StackOverflow):
Public Shared ReadOnly Property AssemblyDirectory() As String
  Get
    Dim codeBase As String = Assembly.GetExecutingAssembly().CodeBase
    Dim uriBuilder As New UriBuilder(codeBase)
    Dim assemblyPath As String = Uri.UnescapeDataString(uriBuilder.Path)
    Return Path.GetDirectoryName(assemblyPath)
  End Get
End Property


Here is the post on StackOverflow that is related:
http://stackoverflow.com/questions/52797/c-how-do-i-get-the-path-of-the-assembly-the-code-is-in

See this forum thread for related info:
http://forums.arcgis.com/threads/17397-Add-In-embedded-Files-and-use-it-s-path

Also, have you looked in your Addin file with a zip viewer to ensure the "Layer Files" folder and files are actually there?

-Jeff
0 Kudos
DuncanHornby
MVP Notable Contributor
Jeff,

Thanks for the advice, I will test that code you suggested. I have looked inside the esriAddin file with winzip and they are in there in.

But I'm having second thoughts about where one is actually "going" to get the layerfiles from. I was thinking that you give it a path name (when I finally get it working) that points inside the esriaddin file but the help file talks about setting the copy to output directory. So when you build the visual studio project and look in the bin directory you get the folder with layer files, the esriaddin file then a bunch of other files, but all these are also inside the esriaddin file. Sooo... if for example I take the esriaddin file only and put it in c:\temp am I developing code that is actually looking for a sub folder in c:\temp rather than inside the esriaddin file?

Duncan
0 Kudos
by Anonymous User
Not applicable
Original User: jeffhamblin

Hi Duncan,

Just think of the "YourAddin.esriaddin" file as a distribution and installation container.

Whether you run it to install it in ArcGIS using the wizard, or just drop it into one of the "well-known" locations, the Addin does not run from the .esriaddin file. For everything in there to be used it needs to be extracted by ArcGIS on first use. Where it all ends up is in the AssemblyCache location. That is where your Addin's DLL and resources can be accessed.

I think you are pretty close to getting this working, once you get the correct path to the Addin in the AssemblyCache.


-Jeff
0 Kudos
DuncanHornby
MVP Notable Contributor
Jeff,

Your code above worked and the path did include the cryptic guid number when I displayed it in a message box. I was then able to connect to the LayerFile.

So thanks for your help! It's a shame that the ESRI help hints at this method of packaging up other files but provides no example code, if they do it's well hidden!

Duncan
0 Kudos
by Anonymous User
Not applicable
Original User: mike.robb

Why is it that I cant just use the directory string pulled, that I have to replace the %7B with { and %7D as }  to get the path to the files pushed along with the addin?

This is what I do...Just never understood why I have to manipulate the string path.



    Private Function GetAssemblyDirectory() As String

        Dim Codebase As String
        Dim URI As UriBuilder
        Dim AssemblyDLLFilePath As String
        Dim AssemblyDirectory As String

        Codebase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase
        URI = New UriBuilder(Codebase)
        AssemblyDLLFilePath = URI.Path.ToCharArray

        AssemblyDirectory = System.IO.Path.GetDirectoryName(AssemblyDLLFilePath)

        AssemblyDirectory = AssemblyDirectory.Replace("%7B", "{")
        AssemblyDirectory = AssemblyDirectory.Replace("%7D", "}")

        Return AssemblyDirectory

    End Function
0 Kudos
JeffreyHamblin
Occasional Contributor
Why is it that I cant just use the directory string pulled, that I have to replace the %7B with { and %7D as }  to get the path to the files pushed along with the addin?

This is what I do...Just never understood why I have to manipulate the string path.


CodeBase is in URI format, so has had certain characters escaped. So it would be better to use the Uri.UnescapeDataString() method to take care of those, rather than just handling the specific ones.

-Jeff
0 Kudos
by Anonymous User
Not applicable
Original User: mike.robb

CodeBase is in URI format, so has had certain characters escaped. So it would be better to use the Uri.UnescapeDataString() method to take care of those, rather than just handling the specific ones.

-Jeff


makes sense...
Used
System.Uri.UnescapeDataString(URI.Path)

Thanks for that tid bit of info 😉
0 Kudos