如何分配Excel VSTO安装的程序集位置?

我正在创建一个用C#编写的文档级工作簿/模板,并使用VSTO安装程序来部署代码。 安装项目后,我有完整的电子表格function,但是,当我将安装的工作表保存或复制到安装文件夹之外的另一个路径时,我收到以下错误:

呃哦!

具有以下完整细节:

Name: From: file:///C:/Users/Kronos/Desktop/ExcelTemplate1.vsto ************** Exception Text ************** System.Deployment.Application.DeploymentDownloadException: Downloading file:///C:/Users/Kronos/Desktop/ExcelTemplate1.vsto did not succeed. ---> System.Net.WebException: Could not find file 'C:\Users\Kronos\Desktop\ExcelTemplate1.vsto'. ---> System.Net.WebException: Could not find file 'C:\Users\Kronos\Desktop\ExcelTemplate1.vsto'. ---> System.IO.FileNotFoundException: Could not find file 'C:\Users\Kronos\Desktop\ExcelTemplate1.vsto'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync) at System.Net.FileWebStream..ctor(FileWebRequest request, String path, FileMode mode, FileAccess access, FileShare sharing, Int32 length, Boolean async) at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean asyncHint) --- End of inner exception stack trace --- at System.Net.FileWebResponse..ctor(FileWebRequest request, Uri uri, FileAccess access, Boolean asyncHint) at System.Net.FileWebRequest.GetResponseCallback(Object state) --- End of inner exception stack trace --- at System.Net.FileWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.FileWebRequest.GetResponse() at System.Deployment.Application.SystemNetDownloader.DownloadSingleFile(DownloadQueueItem next) --- End of inner exception stack trace --- at Microsoft.VisualStudio.Tools.Applications.Deployment.ClickOnceAddInDeploymentManager.GetManifests(TimeSpan timeout) at Microsoft.VisualStudio.Tools.Applications.Deployment.ClickOnceAddInDeploymentManager.InstallAddIn() 

我意识到这是因为.VSTO.manifest.DLL文件没有被正确引用,因为Excel电子表格不再存在于已安装的路径中。 在做了一些研究后,我可以通过更改.xlsx文件中复制/保存的custom.xml文件来手动修复此问题:

 name="_AssemblyLocation">ExcelTemplate1.vsto|ca022788-e7c0-41d8-b8ae-2c0ba9edbbf8|vstolocal 

对此:

 name="_AssemblyLocation">file://c://ExcelTemplate1.vsto|ca022788-e7c0-41d8-b8ae-2c0ba9edbbf8|vstolocal 

由于这对我的客户来说不是一个可行的解决方案,如何使用C#代码或(更优选)安装程序进行上述更改?

注意:我尝试创建自定义安装操作( 根据此MSDN教程 ),其中为CustomActionData设置以下内容:

 /assemblyLocation="[TARGETDIR]ExcelWorkbookProject.dll"/deploymentManifestLocation="[TARGETDIR]ExcelWorkbookProject.vsto"/documentLocation="[TARGETDIR]ExcelWorkbookProject.xlsx" 

无济于事。

您需要按照您引用的MSDN文章中列出的说明进行操作。 然而,这有点令人困惑,文章中有错误。 希望这有助于澄清:

您需要定义类似于文章提供的用户脚本

在文章中,您可以下载包含项目示例的文件。 从那里,您可以针对该cs项目的输出引用Custom Actions 。 创建一个CS类库的新项目,复制以下用于解决您的问题的用户脚本:

 using System; using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Configuration.Install; using System.Linq; using Microsoft.VisualStudio.Tools.Applications; using Microsoft.VisualStudio.Tools.Applications.Runtime; using System.IO; using System.Windows.Forms; namespace AddCustomizationCustomAction { [RunInstaller(true)] public partial class AddCustomization : System.Configuration.Install.Installer { //Note: you'll have to get the Guid from your specific project in order for it to work. The MSDN article show you how. static readonly Guid SolutionID = new Guid("20cb4d1d-3d14-43c9-93a8-7ebf98f50da5"); public override void Install(IDictionary stateSaver) { string[] nonpublicCachedDataMembers = null; // Use the following for debugging during the install //string parameters = "Parameters in Context.Paramters:"; //foreach (DictionaryEntry parameter in Context.Parameters) //{ // parameters = parameters + "\n" + parameter.Key + ":" + parameter.Value; //} //MessageBox.Show(parameters); //MessageBox.Show("total items in parameters: " + Context.Parameters.Count); //MessageBox.Show("Document Manifest Location:" + Context.Parameters["deploymentManifestLocation"]); Uri deploymentManifestLocation = null; if (Uri.TryCreate( Context.Parameters["deploymentManifestLocation"], UriKind.RelativeOrAbsolute, out deploymentManifestLocation) == false) { throw new InstallException( "The location of the deployment manifest " + "is missing or invalid."); } string documentLocation = Context.Parameters["documentLocation"]; if (String.IsNullOrEmpty(documentLocation)) { throw new InstallException( "The location of the document is missing."); } string assemblyLocation = Context.Parameters["assemblyLocation"]; if (String.IsNullOrEmpty(assemblyLocation)) { throw new InstallException( "The location of the assembly is missing."); } // use the following for debugging MessageBox.Show(documentLocation); if (ServerDocument.IsCustomized(documentLocation)) { ServerDocument.RemoveCustomization(documentLocation); } ServerDocument.AddCustomization( documentLocation, assemblyLocation, SolutionID, deploymentManifestLocation, false, out nonpublicCachedDataMembers); stateSaver.Add("documentlocation", documentLocation); base.Install(stateSaver); } public override void Commit(IDictionary savedState) { base.Commit(savedState); } public override void Rollback(IDictionary savedState) { base.Rollback(savedState); } public override void Uninstall(IDictionary savedState) { base.Uninstall(savedState); } } } 

这将覆盖安装程序的安装过程。 base.Install(stateSaver)调用其余代码以正常继续安装。

MSDN文章中的错误:

文章说使用以下内容来安装自定义操作的CustomActionData

 /assemblyLocation="[TARGETDIR].dll"/deploymentManifestLocation="[TARGETDIR].vsto"/documentLocation="[TARGETDIR].xltx" 

但它应该是这样(注意params之间的空格):

 /assemblyLocation="[TARGETDIR].dll" /deploymentManifestLocation="[TARGETDIR].vsto" /documentLocation="[TARGETDIR].xltx" 

这应该可以解决您的问题,但是在重建安装程序之前,请确保将Excel项目的任何更改重建为发布版本,因为它指向的是发行版,而不是调试版。