0

I am currently trying to deploy an Outlook VSTO Add In that I developed with Visual Studio. As long as I debug it with Visual Studio everything works perfectly fine. However, I ran into sume trouble as soon as I deployed it and installed it via the created .exe installer.

I pinned down the problem and it seems to be an additional .json file that the Add In opens and reads on start up. I created this file and store it in my Visual Studio Project Directory to save some data that the Add In needs.

I figured that the deployed and installed Add In might have difficulties finding the file as there might be a new filepath. So I tried the following:

  1. In the file properties set in Visual Studio I changed the settings to always copy the file to the output directory and build its content
  2. I changed the filepath from which the file is opened from just "file.json" to Dim jsonFilePath As String = System.AppDomain.CurrentDomain.BaseDirectory + "file.json"

Both approaches did not work. So my question is if anyone has ideas on how I could fix that.

Many thanks in advance!

Update: Due to your ansers I figured it out. In VB.Net I now use:

Dim codebase As New System.Uri(Assembly.GetExecutingAssembly().CodeBase)
Dim filepath As String = Path.Combine(codebase.AbsolutePath, "..", "file.json")
Jonas
  • 43
  • 4
  • Do you see an error message? If yes, could you add it to your question?. Is the file.json present in the directory where the add-in is installed (on the customer pc). Both methods try to read the file from the same folder where the add in is installed – Steve May 22 '23 at 14:44
  • I don't see any error messages unfortnuately. The issue is that after installing, I can't use the Add In. So it is deactivated by default and I can't manually add it via the COM Add In manager. But when I deploy everything after removing any reference to the .json file it works as it should. Do you know what the usual folder is to which these add ins are installed? Then I can check if they are present. – Jonas May 22 '23 at 15:08

2 Answers2

1

Most likely you are running into the shadow copy problem. Use Assembly.GetExecutingAssembly().CodeBase instead:

string codebase = Assembly.GetExecutingAssembly().CodeBase;
var vUri = new UriBuilder(codebase);
string vPath = Uri.UnescapeDataString(vUri.Path + vUri.Fragment);
string directory = Path.GetDirectoryName(vPath);
if (!string.IsNullOrEmpty(vUri.Host)) directory = @"\\" + vUri.Host + directory;
string jsonFilePath = Path.Combine(directory, "file.json");
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
1

That is widely spread issue when dealing with assembly path/location at runtime. You can use the following code to get the assembly location at runtime:

string path = (new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;

See Getting the path of the current assembly for more information about that.

Also, as a possible workaround, you may consider adding the installation directory path to the windows registry at the installation time of your add-in (via custom actions in your MSI installer). So, at any point of time you could read the path and deal with local files without relying on the assembly path.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Thank you very much! Do you by any chance have a link to some resources on how I set up an MSI installer with visual studio? – Jonas May 23 '23 at 07:49
  • See [Deploying a VSTO Solution Using Windows Installer](https://learn.microsoft.com/en-us/visualstudio/vsto/deploying-a-vsto-solution-by-using-windows-installer?view=vs-2022) – Eugene Astafiev May 23 '23 at 10:23