default lookup location for external files

1628
3
12-30-2016 11:27 AM
andresarias
New Contributor II

Hello, I'm reading a text file to populate a combo box so that its items can be easily modified. Which is the default location for such a file if for example I use: new System.IO.StreamReader("myaddin.txt"); ? I've tried putting the text file in the same folder where the myaddin.esriAddInX is (which would be my preferred location) with no success. Where can I have an explanation on what is the process that add-ins go through to search for a file?

#arcgisprosdk#addin#c sharp

0 Kudos
3 Replies
CharlesMacleod
Esri Regular Contributor

Assuming that you want to deploy your file with the Addin archive (and not separately) you can find details here:

ProGuide-content-and-image-resources#document-content.

In brief, you set the "Copy to Output Directory" property on the text file to "Copy Always" and read the file from the assembly cache. There is code in the ProGuide showing you what to do.

However, in your case, that is probably overkill. Given your content is text I would add the "myaddin.txt" as a Resource to your solution:

  • Add a resource file to your VS project (usually called Resource1.resx by default)
  • Open it in the Visual Studio Designer (double click on it in the solution explorer)
  • Under the "Add Resource" menu select "Add New Text File" and name it "myaddin" (see image)
    • (or use the 'add existing file...' - your choice)
  • The new text file will open in Visual Studio, copy and paste your content into the text file
  • Save and build the project

Now, in your code you can access the file content directly off the resource class like so:

var file_content  = Resource1.myaddin;

File-as-resource

0 Kudos
andresarias
New Contributor II

Thank you Charles, but neither is what I'm looking for. I want the text file to be external to the Addin so that the lines (each line is an item) can be edited (customized) by each Addin user. The users won't have Visual Studio to access and edit any embedded resource. The "Access Content" link you sent me has a similar concept to what I want which is to get the path where the Addin is located in run time that would then be useful to access the Addin accompanying text file. However it is reading the assembly path and not the actual location of the distributed Addin.

0 Kudos
CharlesMacleod
Esri Regular Contributor

If you need to know the location of an external file it is best to have users install the file to a well-known location (well-known to your Add-in). DotNet has a special enumeration for a series of "special folders" the System.Environment.SpecialFolder Enumeration that can be resolved on any machine. Users copy your file to a particular "special" location (eg My Documents) within which your Add-in always looks for the file. If you desire something more sophisticated, then an installer is required. An installer copies the file to a folder location of the user's choice and records the location in a registry key which your Add-in accesses. The installer would also install your Add-in. This is a very common pattern for file deployment and discovery. Visual Studio has a project template for creating installers called "Setup and Deployment". You'll find it under "Other Project Types".

0 Kudos