以编程方式从Ribbon按钮的回调中访问VSTO AddIn项目中包含的文件。 [英] programmatically accessing files included in a VSTO AddIn project from a Ribbon button's callback.

查看:215
本文介绍了以编程方式从Ribbon按钮的回调中访问VSTO AddIn项目中包含的文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的MS Word VSTO AddIn项目的根文件夹中有 testFile.txt 文件。在Visual Studios的调试模式下,我可以通过编程方式访问此文件的路径,如下所示。 [注意:以下代码位于此VSTO项目中创建的自定义功能区按钮的回调
内]:

Suppose I have a testFile.txt file in my MS Word VSTO AddIn project's root folder. In Visual Studios's debug mode, I can access this file's path programmatically as follows. [Note: The following code is inside a callback of a Button of Custom Ribbon created in this VSTO project]:

string strFilePath = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory())) + "\\testFile.txt";

众所周知,当您通过单击VS中Build Meun下拉列表的 Rebuild Project 子菜单来构建项目时,将在Word中创建AddIn(带有自定义功能区)。但是,当我单击该自定义功能区中的相应按钮时,
上面的代码失败,因为它无法找到testFile.txt的路径。 问题:如何修改上述代码,以便在构建项目时(因此在Word应用程序中创建Addin)单击Word中自定义功能区中相应按钮
的回调应用程序找到testFile.txt?

As we know, when you build the project by clicking on the Rebuild Project submenu of the Build Meun dropdown in VS, the AddIn (with a custom Ribbon) gets created in Word. But, when I click on the corresponding button in that custom Ribbon, the above code fails since it can't find the path of the testFile.txt. Question: How the above code can be modified so when the project is built (and hence the Addin is created in Word application) the click callback of the corresponding button in the custom ribbon in Word application finds the testFile.txt?

推荐答案

//use CodeBase instead of Location because of Shadow Copy.
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 strFilePath = Path.Combine(directory, "TestFile.txt");


这篇关于以编程方式从Ribbon按钮的回调中访问VSTO AddIn项目中包含的文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆