• 给VSTO 解决方案指定产品名、发布者以及其他属性信息


    原文地址:Office Development with Visual Studio : Specifying a Product Name, Publisher Name and other properties for VSTO solutions (Saurabh Bhatia)

    作者: Saurabh Bhatia (微软VSTO项目经理)

    发布时间:2008年6月8号

    现在越来越多的用户正在使用Visual Studio 2008 开发Office 2007解决方案。许多用户询问如何为这样的解决方案指定属性,如产品名,发布者等。不过Visual Studio IDE 里做不了这个事。在这篇文章里,我将说明几种方法来指定属性。

    具体来说我们将关注的几个属性:

    ² Product Name – 这就是VSTO 解决方案出现在添加/删除程序中的名字;

    ² Publisher Name – 显示在添加/删除程序中;

    1 – Vista中添加/删除程序显示了Product Name 和发布者

    ² Friendly Name – 这将显示在Office 加载项对话框以及安装解决方案时出现的寻求信任对话框,它一般和产品名一致。

    ² Office Application Description – Office 加载项的说明;

    图二 – Excel 加载项对话框显示了Friendly Name Office 加载项说明

    ² SupportURL – 出现在安装寻求信任对话框上,用户点击加载项名的连接即可访问和解决方案相关的网站。

     

    图三显示用户友好名以及相关网站的连接

    最简单的方法是修改部署清单文件和应用程序清单文件。(.vsto 文件和.dll.manifest 文件)

    指定产品名、发布者和帮助网站

    产品名、发布者以及帮助网站这些信息都可以在部署清单文件中指定,也就是在发布解决方案时生成的.vsto 文件。你可以在文本编辑器中打开这个文件并在<description> 标签中指定相关信息。

    <description asmv2:publisher="Visual Studio (BizApps)" asmv2:product="My VSTO Solution" asmv2:supportUrl="http://www.microsoft.com/" xmlns="urn:schemas-microsoft-com:asm.v1" />

    修改好部署清单文件后必须重新给它签名。签名使用Manifest Generation and Editing Tool (mage.exe)来完成。 .NET Framework SDKWindows SDK或者Visual Studio 2008中都带有这个工具。比如:打开Visual Studio 命令行快捷方式,使用mage.exe 来给部署清单文件重新签名。mage.exe 的用法:

    mage – sign deploymentmanifest.vsto –Certfile Certificate.pfx

    例如:

    mage – sign MyExcelAddin.vsto – CertFile MyCert.pfx

    指定Friendly Name Office 应用程序说明

    Friendly Name Office 应用程序说明都在应用程序清单文件的VSTO3 命名空间下。

    应用程序清单文件(如MySolution.dll.manifest)一般在 Application Files"MySolution_X_X_X_X" 文件夹下, X表示已发布解决方案的最新版。

    打开应用程序清单文件修改一下属性:

    <vstov3:customization xmlns:vstov3="urn:schemas-microsoft-com:vsto.v3">
    <vstov3:appAddIn application="Excel" loadBehavior="3" keyName="ExcelAddIn9">
    <vstov3:friendlyName>My VSTO Solution</vstov3:friendlyName>
    <vstov3:description>A Sample Add-in created to show the add-in properties work</vstov3:description>
    </vstov3:appAddIn>

    </vstov3:customization>

    修改了应用程序清单文件(MySolution.dll.manifest)以后,必须给它也重签名。而且你还需要刷新部署清单文件来让它产生新的哈希值。最后别忘了部署清单文件(.vsto)重新签名。

    以上编辑应用程序清单文件后的三个步骤可以用mage 来完成:

    1. 应用程序清单重签名:

    Mage -sign applicationmanifest.dll.manifest -Certfile certfile.pfx
    Example:
    Mage -sign “Application Files"MyVsto_1_0_0_0"myvsto.dll.manifest” -Certfile .."myvsto.pfx

    2. 更新部署清单文件:

    Mage -update deploymentmanifest.vsto -AppManifest applicationmanifest.manifest
    Example:
    Mage -update myvsto.vsto - AppManifest “Application Files"MyVsto_1_0_0_0"myvsto.dll.manifest”

    3. 部署清单重签名:

    Mage -sign deploymentmanifest.vsto -Certfile Certificate.pfx
    Example:
    Mage -sign MyExcelAddin.vsto -CertFile MyCert.pfx

    现在你的VSTO 解决方案就有了这些指定的信息了。但是这个方法的一个缺点就是每发布一次都要手工去做这些步骤。如果不想执行这些手工的步骤,你可以修改VSTO 构建目标文件(build targets),这样在VS发布的时候这些信息就会自动加入了。大致的想法是修改VS的项目文件(.vbproj 或者.csproj, 构建任务会逐个的读取这些属性信息,在每次发布项目时会把他们自动加入清单文件。

    修改targets 文件是有风险的;如果你改错了某些东西,你的项目都会构建失败。因此为了安全起见,在修改前别忘了备份。我们修改的不是主要的target文件而是它的一个备份,任何你想指定发布属性的项目都会使用新的修改过的target文件。

    修改target文件前另一个要考虑的是:如果你使用修改过的target文件,并且之后又升级了VS,那么你的项目也将构建不成功。如果你升级VS,你必须在新的target文件做相应的修改。

    创建备份、修改targets 文件

    我们将修改的文件是:

    %ProgramFiles%"MSBuild"Microsoft"VisualStudio"v9.0"OfficeTools"Microsoft.VisualStudio.Tools.Office.Office2007.targets

    打开targets文件

    1. 创建备份,把备份放在安全的地方(这点太重要了)

    2. 再创建一个备份,并修改它。

    a)         创建此文件的备份,把它存在Visual Studio 2008 文件夹下。

    Vista下:C:"Users"username"Documents"Visual Studio 2008
    XP : C:"My Documents"Visual Studio 2008

    b)         重命名为:Microsoft.VisualStudio.Tools.Office.Office2007_Properties.targets

    c)         修改这个文件。

    3. 用你喜欢的编辑器来打开Microsoft.VisualStudio.Tools.Office.Office2007_Properties.targets

    修改targets文件来显示产品名

    在这个例子中,Friendly Name 和产品名一致。

    1. 找到Add-in options部分的注释 <!-- Add-In options -->

    此注释后边一般跟着 LoadBehavior 标签:<LoadBehavior>3</LoadBehavior>

    2.  Add-in option部分,LoadBehavior 标签前加上:

    <ProductName Condition=" '$(ProductName)' == '' ">$(TargetName)</ProductName>
    上面的引号是这样的:
    Condition = <
    双引号><单引号> $(ProductName) <单引号> == <单引号><单引号><双引号>

    3. 查找所有的”FriendlyName”, FriendlyName="$(TargetName)"替换成FriendlyName = “$(ProductName)”, 总共有3处。

    4. 查找以下Target: GenerateDeploymentManifestForPublishing:

    <Target Name="GenerateDeploymentManifestForPublishing">

    5. 在这个target 中有这样一个taskGenerateDeploymentManifest

    <GenerateDeploymentManifest
    EntryPoint="@(ApplicationManifestWithPathForPublishingCollection)"
    AssemblyName="$(DeploymentManifestFileName)"
    AssemblyVersion="$(PublishVersion)"
    MapFileExtensions="$(MapFileExtensions)"
    OutputManifest="@(DeploymentManifestWithPathForPublishingCollection)"
    Platform="$(PlatformTarget)"
    Install="false"
    />

    6. 给这个任务加一个Product 属性:

    <GenerateDeploymentManifest
    EntryPoint="@(ApplicationManifestWithPathForPublishingCollection)"
    AssemblyName="$(DeploymentManifestFileName)"
    AssemblyVersion="$(PublishVersion)"
    Product="$(ProductName)"
    MapFileExtensions="$(MapFileExtensions)"
    OutputManifest="@(DeploymentManifestWithPathForPublishingCollection)"
    Platform="$(PlatformTarget)"
    Install="false"
    />

     

    修改targets 文件来显示发布者

    1.         查找以下Target: GenerateDeploymentManifestForPublishing:

    <Target Name="GenerateDeploymentManifestForPublishing">

    2.         在这个target 中有这样一个taskGenerateDeploymentManifest

    <GenerateDeploymentManifest
    EntryPoint="@(ApplicationManifestWithPathForPublishingCollection)"
    AssemblyName="$(DeploymentManifestFileName)"
    AssemblyVersion="$(PublishVersion)"
    Product="$(ProductName)"
    MapFileExtensions="$(MapFileExtensions)"
    OutputManifest="@(DeploymentManifestWithPathForPublishingCollection)"
    Platform="$(PlatformTarget)"
    Install="false"
    />

    3.         添加Publisher 属性到这个task

    <GenerateDeploymentManifest
    EntryPoint="@(ApplicationManifestWithPathForPublishingCollection)"
    AssemblyName="$(DeploymentManifestFileName)"
    AssemblyVersion="$(PublishVersion)"
    Product="$(ProductName)"
    Publisher="$(PublisherName)"
    MapFileExtensions="$(MapFileExtensions)"
    OutputManifest="@(DeploymentManifestWithPathForPublishingCollection)"
    Platform="$(PlatformTarget)"
    Install="false"
    />

    修改targets文件来显示SupportURL

    1.       查找 Target InitializePublishProperties<Target Name="InitializePublishProperties">

    它在CreateProperty task之后:

    <CreateProperty Value="Application Files">
         <Output PropertyName="ApplicationFilesFolderName" TaskParameter="Value"/>
    </CreateProperty>

    2.       CreateProperty 后添加:

    <FormatUrl InputUrl="$(SupportUrl)"> 
    <Output TaskParameter="OutputUrl" PropertyName = "_DeploymentFormattedSupportUrl"/>
    </FormatUrl>

    3.       查找以下Target: GenerateDeploymentManifestForPublishing:

    <Target Name="GenerateDeploymentManifestForPublishing">

    4.       在这个target 中有GenerateDeploymentManifest task

    <GenerateDeploymentManifest
    EntryPoint="@(ApplicationManifestWithPathForPublishingCollection)"
    AssemblyName="$(DeploymentManifestFileName)"
    AssemblyVersion="$(PublishVersion)"
    Product="$(ProductName)"
    Publisher="$(PublisherName)"
    MapFileExtensions="$(MapFileExtensions)"
    OutputManifest="@(DeploymentManifestWithPathForPublishingCollection)"
    Platform="$(PlatformTarget)"
    Install="false"
    />

    5.       添加SupportURL信息到这个task

    GenerateDeploymentManifest
    EntryPoint="@(ApplicationManifestWithPathForPublishingCollection)"
    AssemblyName="$(DeploymentManifestFileName)"
    AssemblyVersion="$(PublishVersion)"
    Product="$(ProductName)"
    Publisher="$(PublisherName)"
    SupportUrl="$(_DeploymentFormattedSupportUrl)"
    MapFileExtensions="$(MapFileExtensions)"
    OutputManifest="@(DeploymentManifestWithPathForPublishingCollection)"
    Platform="$(PlatformTarget)"
    Install="false"
    />

    修改targets 文件来显示Office 应用程序说明

    1. 在文件中查找 “OfficeApplicationDescription”;

    2. 找到如下信息:

    <OfficeApplicationDescription Condition="'$(VSTO_ProjectType)' == 'Application'">$(TargetName) - $(OfficeApplication) add-in created with Visual Studio Tools for Office</OfficeApplicationDescription>

    3. 把它修改成:

    <OfficeApplicationDescription Condition="'$(OfficeApplicationDescription)' == '' and '$(VSTO_ProjectType)' == 'Application'">$(TargetName) - $(OfficeApplication) add-in created with Visual Studio Tools for Office</OfficeApplicationDescription>

    注意引号是这样的:

    Condition = <双引号><单引号> $(OfficeApplicationDescription) <单引号> == <单引号><单引号>

     

    至此你已经完成了修改targets 文件,下一步是修改项目文件并指定这些属性值。你必须修改每一个项目文件来指定这些属性值,你可以用文本编辑器来打开项目文件。

    修改项目文件是为了使用我们刚刚创建的新的targets文件。

    修改项目文件来应用新的targets文件

    1. 在项目文件中查找以下注释:

    <!-- Include additional build rules for an Office application add-in. -->

    把原来指向的targets文件<Import Project="$(MSBuildExtensionsPath)"Microsoft"VisualStudio"v9.0"OfficeTools"Microsoft.VisualStudio.Tools.Office.Office2007.targets" />

    替换为新的targets文件:<Import Project="c:"users"username"documents"Visual Studio 2008" Microsoft.VisualStudio.Tools.Office.Office2007_Properties.targets" />

    在项目文件中指定属性

    在第一个<PropertyGroup> 标签中添加如下的属性信息:

    <ProductName>My VSTO Solution</ProductName>

    <PublisherName>Visual Studio (BizApps)</PublisherName>

    <OfficeApplicationDescription>A Sample Add-in created to show the add-in properties work</OfficeApplicationDescription>

    <SupportUrl>http://www.microsoft.com</SupportUrl>

    添加完这些属性你就可以保存并用VS打开它了。打开项目时你会看到警告说,你的项目已被修改来使用自定义的构建步骤,选择普通方式打开项目。从现在开始,在发布时这些属性将会被自动加入到清单文件中。如果你更新了这些属性信息,那么在发布时他们也会被自动更新到清单文件中。

    最后我想说的是,修改targets 文件时一个相当长且复杂的过程,但是如果你成功的修改了它,那么你将很方便的为人和项目指定属性信息。

    希望这篇文章对你有用。有任何问题请留言。

  • 相关阅读:
    async/await
    js继承
    js处理上下文代码的2个阶段
    js相关地址
    vue自定义指令钩子函数
    vue 组件
    vue methods和computed,v-show和v-if
    vue 生命周期
    DOM操作——怎样添加、移除、移动、复制、创建和查找节点
    js 传递参数
  • 原文地址:https://www.cnblogs.com/monster1799/p/1310866.html
Copyright © 2020-2023  润新知