App.Path in VB .NET, Help Please??

3300
7
01-12-2012 06:32 AM
Highlighted
New Contributor II
Hello:

I have a code in vb6 that I'm upgrading to VB.Net as an ESRI Add-In. It has a config.ini file that the user can modify because we would like to use this office wide connecting to different sql servers. Anyway I cannot get the add-in to find its config.ini file.

I've already tried

path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)

and

Module modAppInfos
Public Function App_Path() As String
App_Path = CStr(Nothing)
App_Path = System.Reflection.Assembly.GetExecutingAssembly.GetName().CodeBase
If StrComp(LCase(Left(App_Path, 8)), "file:///") = 0 Then
App_Path = Mid(App_Path, 9) 'file:/// entsorgen
End If
App_Path = StripFileName(App_Path)
End Function
End Module

And neither of them worked.
Any help would be welcome.

Thanks
Nahide
Reply
0 Kudos
7 Replies
Highlighted
Regular Contributor III
Exactly what do you mean when you say neither worked?

path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)

This returns the full path to the directory that contains your assembly dll.  If the config file is in this same directory then all you need to do is append the filename to this path.
Reply
0 Kudos
Highlighted
MVP Frequent Contributor
Here's what I have done with Neil's approach:

I use this as a Private function throughout my assemblies and works well when I have support files that need to be accessed or written to and are/have been deployed with the application when installed.

Private Function GetAppPath() As String

        Dim path As String
        path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
        Return path

End Function


So to use this, here is an example of how I might setup a connection string to an Excel file that was included in the original deployment/install of the application:


Dim m_sConn1 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & GetAppPath() & "\" & inDist & ".xlsx;;Extended Properties=""Excel 12.0 Xml;HDR=Yes"""

Dim ExcelConnection As New System.Data.OleDb.OleDbConnection(m_sConn1)
ExcelConnection.Open()
Reply
0 Kudos
Highlighted
New Contributor II
Exactly what do you mean when you say neither worked?

path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)

This returns the full path to the directory that contains your assembly dll.  If the config file is in this same directory then all you need to do is append the filename to this path.


To Neil:
I've found your post from 2006 yesterday and it was explaining that I need to compile the code and then put the config file within the directory where the add-in or the dll is in. I did it and still didn't work. But it was really late in the day so it is very possible that I've made a mistake while I was modifying the code.  So I will try again and let you know.

To James:
I'll try that GetPath function since it uses Neil's approach.

Thank you both for replying so quickly.
Nahide
Reply
0 Kudos
Highlighted
New Contributor II
Well it did not work. This is the error I'm getting.

[ATTACH=CONFIG]11137[/ATTACH]


The config.ini file is in a folder with the add-in in C drive, but GetPath is trying to find it in AssemblyCache etc. folder.


Thanks
Nahide
Reply
0 Kudos
Highlighted
Regular Contributor III
I don't work with addins so I can't tell you exactly, but I believe ArcGIS installs addins to a common folder.  You can't install an addin anywhere on your computer like you can when you install normal programs.  Your config file will need to go into this same location.  Again, I don't know the exact details but I believe you can package the config file up with the rest of the assembly files and they will all be unpacked to the same location.  If you leave the config file where it currently is then you will have to use some other method to find it.
Reply
0 Kudos
Highlighted
New Contributor II
Neil:

I'm going to look into creating an config xml rahter than an ini file. Maybe that will work because there is standard esriconfig xml that compiles with the addin and still can be opened up by the user. I'll reply to this thread when I find an answer.

Thanks again for your help

Nahide
Reply
0 Kudos
Highlighted
Occasional Contributor
If you want to use a config XML, I recommend adding your own rather than using the Config.esriaddinx file.  This keeps things clean.  To add your own file in Visual Studio 2010:


  1. In Solution Explorer, right-click your add-in, point to Add, and click New Item.

  2. With the Common Items category selected, choose XML File.  Give it a name and click Add.

  3. Click the XML file in the Solution Explorer, and then in the Properties window, set Copy to Output Directory to "Copy always."



You should now be able to use the GetAppPath code that was previously posted along with IO.Path.Combine to create the path to the XML file. 

For more on adding your own files for use with add-ins, see
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Advanced_add_in_concept...

The article says, "You can create additional folders and files under the Install folder. This is useful in cases where you want to ship data as part of your add-in. If you're adding data through your Visual Studio project, set the Copy to Output Directory property to the Copy always setting..."
Reply
0 Kudos