在部署的 Office 插件中遇到文件路径问题。

huangapple go评论45阅读模式
英文:

Trouble with the filepath of a deployed Office Add In

问题

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.

我目前正尝试部署我用Visual Studio开发的Outlook VSTO插件。只要我在Visual Studio中调试它,一切都运行得非常完美。但是,一旦我部署它并通过创建的.exe安装程序安装它,我就遇到了一些问题。

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.

我锁定了问题,似乎是插件在启动时打开并读取的一个额外的.json文件。我创建了这个文件并将其存储在我的Visual Studio项目目录中,以保存插件所需的一些数据。

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. 在Visual Studio中设置的文件属性中,我更改了设置,始终将文件复制到输出目录并构建其内容

  3. 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"

  4. 我将打开文件的文件路径从"file.json"更改为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:

更新:由于您的回答,我已经弄清楚了。在VB.Net中,我现在使用以下代码:

Dim codebase As New System.Uri(Assembly.GetExecutingAssembly().CodeBase)
Dim filepath As String = Path.Combine(codebase.AbsolutePath, "..", "file.json")

英文:

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")

答案1

得分: 1

你可能遇到了影子拷贝的问题。请使用 Assembly.GetExecutingAssembly().CodeBase 替代:

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");
英文:

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");

答案2

得分: 1

以下是翻译好的部分:

这是在运行时处理程序集路径/位置时广泛存在的问题。 您可以使用以下代码在运行时获取程序集位置:

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

有关更多信息,请参阅获取当前程序集的路径

另外,作为可能的解决方法,您可以考虑在插件安装时将安装目录路径添加到Windows注册表中(通过MSI安装程序中的自定义操作)。 因此,随时都可以读取该路径并处理本地文件,而无需依赖于程序集路径。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月22日 22:28:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307221.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定