如何使用Visual Studio 2017注册"自定义工具"以使其工作?

Amn*_*oel 6 c# registry customtool visual-studio-2017

背景:我们有一个自定义工具,它接受xml输入并生成cs输出.自定义工具需要向 Visual Studio 注册才能使其与该版本的visual studio一起使用.

我们做了什么:我们已经使用Visual Studio 2015 完成了自定义工具注册,该工作正常.但现在问题出在Visual Studio 2017上.

问题:所以在我的研究到目前为止,我发现到Visual Studio的2015年,VS有哪些被允许注册工具直接注册表项,而是从VS 2017年,微软已经取得方式的变化注册表项如何存储(很好看的到了解VS2017中的变化).

如果我打开VS 2017并尝试运行自定义工具,那么我得到错误

在此系统上找不到自定义工具"工具名称".

在此输入图像描述

这是显而易见的,因为自定义工具尚未在VS 2017中注册工作.

我试图跟随这个说要将.bin文件加载到注册表的人,但他也说它禁止启动VS 2017.为了启动VS,我们必须卸载配置单元.研究表明,.bin文件可以根据安装的VS的类型(企业,专业等)在不同的位置.

有没有人这样做过?

TIA

小智 18

您可能必须通过创建Visual Studio扩展(VSIX)来遵循不同的方法,下面我已经详细解释了它,希望它有所帮助.

如何在Visual Studio 2017中创建自定义工具或单个文件生成器:

在VS2017之前创建一个自定义工具需要实现接口IVsSingleFileGenerator和代码来注册和注销系统注册表中的自定义工具,但在VS2017中,Microsoft已经更改了整个注册表结构.更改是,VS将使注册表项进入私有注册表,以便系统注册表不会搞砸.以前注册表项是在系统注册表中创建的,现在它们已经完成了

C:\用户\某某\应用程序数据\本地\微软\ VisualStudio的\ 15.0_xx\privateregistry.bin

Visual Studio 2017还支持直接通过Visual Studio本身运行它来测试您的工具(F5),它启动另一个名为Visual Studio实验实例的Visual Studio实例,并且您的工具可以在其中进行测试,因为它使注册表项成为

C:\用户\某某\应用程序数据\本地\微软\ VisualStudio的\ 15.0_xxExp\privateregistry.bin

按照以下步骤在VS2017中创建自定义工具:

  1. 我们需要创建一个VSIX扩展
  2. 添加新的Visual Studio包
  3. 实行 IVsSingleFileGenerator
  4. 添加注册表项代码
  5. 通过在VS2017中运行它来编译和测试该工具
  6. 双击生成的.VSIX文件安装该工具

我们将创建一个扩展/自定义工具作为名为"CountLines"的示例,它将读取一个文件(将Custom Tool属性设置为CountLines)并生成一个包含文件中行数的XML文件.例如<LineCount>1050</LineCount>

1.创建VSIX扩展 为了创建扩展,您必须安装Visual Studio Extensibility Tools,它作为Visual Studio安装程序中的可选功能包含在内.如果未安装,您也可以通过修改VS 2017安装程序来安装它.通过选择创建新的VSIX(Visual Studio扩展)项目

新项目 - >可扩展性 - > VSIX项目

给它一些名字,如"CountLinesVSIX".

2.添加新的Visual Studio包 创建VSIX项目后,通过选择向其添加新的Visual Studio包

添加 - >新项 - >可扩展性 - > Visual Studio包

将其命名为"CountLines.cs".在CountLines.cs我们需要删除现有的代码,并与我们的代码替换它IVsSingleFileGenerator的实现

3.实现IVsSingleFileGenerator 编写接口的自定义实现IVsSingleFileGenerator,我们的示例代码如下

using System;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System.Text;

namespace CountLinesVSIX
    {
    [PackageRegistration(UseManagedResourcesOnly = true)]
    [InstalledProductRegistration( "CountLines", "Generate XML with line count", "1.0")] 
    [Guid("202E7E8B-557E-46CB-8A1D-3024AD68F44A")]
    [ComVisible(true)]
    [ProvideObject(typeof(CountLines))]
    [CodeGeneratorRegistration(typeof(CountLines), "CountLines", "{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}", GeneratesDesignTimeSource = true)]
    public sealed class CountLines : IVsSingleFileGenerator
    {

        #region IVsSingleFileGenerator Members

        public int DefaultExtension(out string pbstrDefaultExtension)
        {
            pbstrDefaultExtension = ".xml";
            return pbstrDefaultExtension.Length;
        }

        public int Generate(string wszInputFilePath, string bstrInputFileContents,
          string wszDefaultNamespace, IntPtr[] rgbOutputFileContents,
          out uint pcbOutput, IVsGeneratorProgress pGenerateProgress)
        {
            try
            {
                int lineCount = bstrInputFileContents.Split('\n').Length;
                byte[] bytes = Encoding.UTF8.GetBytes("<LineCount>" + lineCount.ToString() + "</LineCount>" );
                int length = bytes.Length;
                rgbOutputFileContents[0] = Marshal.AllocCoTaskMem(length);
                Marshal.Copy(bytes, 0, rgbOutputFileContents[0], length);
                pcbOutput = (uint)length;
            }
            catch (Exception ex)
            {
                pcbOutput = 0;
            }
            return VSConstants.S_OK;
        }

        #endregion
    }
}
Run Code Online (Sandbox Code Playgroud)

我们需要为我们的扩展提供一个唯一的GUID,例如上面的代码中的一个[Guid("202E7E8B-557E-46CB-8A1D-3024AD68F44A")].通过选择"工具 - >创建GUID",可以从VS2017 创建GUID.选择GUID格式作为注册表格式.请注意,上面提到的GUID代码没有花括号.

[ComVisible(true)] COM Interops需要它

[CodeGeneratorRegistration(typeof(CountLines), "CountLines", "{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}", GeneratesDesignTimeSource = true)]是一个类属性,其中包含用于注册工具的代码.参数是GeneratorType,GeneratorName和C#语言GUID

您还可以从支持自定义TextTemplate格式的"TemplatedCodeGenerator"派生,这可能需要一些额外的代码实现.

4.添加注册表项代码 使用以下代码创建新的类文件,将其命名为CodeGeneratorRegistrationAttribute.cs

using System;
using System.Globalization;
using Microsoft.VisualStudio.Shell;

namespace CountLinesVSIX
{

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class CodeGeneratorRegistrationAttribute : RegistrationAttribute
    {
        private string _contextGuid;
        private Type _generatorType;
        private Guid _generatorGuid;
        private string _generatorName;
        private string _generatorRegKeyName;
        private bool _generatesDesignTimeSource = false;
        private bool _generatesSharedDesignTimeSource = false;

        public CodeGeneratorRegistrationAttribute(Type generatorType, string generatorName, string contextGuid)
        {
            if (generatorType == null)
                throw new ArgumentNullException("generatorType");
            if (generatorName == null)
                throw new ArgumentNullException("generatorName");
            if (contextGuid == null)
                throw new ArgumentNullException("contextGuid");

            _contextGuid = contextGuid;
            _generatorType = generatorType;
            _generatorName = generatorName;
            _generatorRegKeyName = generatorType.Name;
            _generatorGuid = generatorType.GUID;
        }

        /// <summary> 
        /// Get the generator Type 
        /// </summary> 
        public Type GeneratorType
        {
            get { return _generatorType; }
        }

        /// <summary> 
        /// Get the Guid representing the project type 
        /// </summary> 
        public string ContextGuid
        {
            get { return _contextGuid; }
        }

        /// <summary> 
        /// Get the Guid representing the generator type 
        /// </summary> 
        public Guid GeneratorGuid
        {
            get { return _generatorGuid; }
        }

        /// <summary> 
        /// Get or Set the GeneratesDesignTimeSource value 
        /// </summary> 
        public bool GeneratesDesignTimeSource
        {
            get { return _generatesDesignTimeSource; }
            set { _generatesDesignTimeSource = value; }
        }

        /// <summary> 
        /// Get or Set the GeneratesSharedDesignTimeSource value 
        /// </summary> 
        public bool GeneratesSharedDesignTimeSource
        {
            get { return _generatesSharedDesignTimeSource; }
            set { _generatesSharedDesignTimeSource = value; }
        }


        /// <summary> 
        /// Gets the Generator name  
        /// </summary> 
        public string GeneratorName
        {
            get { return _generatorName; }
        }

        /// <summary> 
        /// Gets the Generator reg key name under  
        /// </summary> 
        public string GeneratorRegKeyName
        {
            get { return _generatorRegKeyName; }
            set { _generatorRegKeyName = value; }
        }

        /// <summary> 
        /// Property that gets the generator base key name 
        /// </summary> 
        private string GeneratorRegKey
        {
            get { return string.Format(CultureInfo.InvariantCulture, @"Generators\{0}\{1}", ContextGuid, GeneratorRegKeyName); }
        }
        /// <summary> 
        ///     Called to register this attribute with the given context.  The context 
        ///     contains the location where the registration inforomation should be placed. 
        ///     It also contains other information such as the type being registered and path information. 
        /// </summary> 
        public override void Register(RegistrationContext context)
        {
            using (Key childKey = context.CreateKey(GeneratorRegKey))
            {
                childKey.SetValue(string.Empty, GeneratorName);
                childKey.SetValue("CLSID", GeneratorGuid.ToString("B"));

                if (GeneratesDesignTimeSource)
                    childKey.SetValue("GeneratesDesignTimeSource", 1);

                if (GeneratesSharedDesignTimeSource)
                    childKey.SetValue("GeneratesSharedDesignTimeSource", 1);

            }
        }

        /// <summary> 
        /// Unregister this file extension. 
        /// </summary> 
        /// <param name="context"></param> 
        public override void Unregister(RegistrationContext context)
        {
            context.RemoveKey(GeneratorRegKey);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的代码将确保您的条目是VS私人注册表

5.通过在VS2017中运行来编译和测试该工具 您可以在" source.extension.vsixmanifest "中添加"安装目标",以确保您的extesion支持不同的VS2017版本.在VS 2017中运行您的工具以测试它是否按预期工作.运行VSIX后,Visual Studio实验实例将安装扩展并将其注册到注册表"C:\ Users\xyz\AppData\Local\Microsoft\VisualStudio\15.0_xxExp\privateregistry.bin".您可以通过选择"工具 - >扩展和更新"来查看已安装的扩展.要测试该工具,我们必须打开一个虚拟项目,在解决方案资源管理器中选择一个文件,转到其属性并将自定义工具属性更新为"CountLines".完成此操作后,VS将在后台运行该工具并生成输出,在我们的示例中,它将在所选文件下生成一个xml文件.或者,一旦设置了自定义工具属性,您可以右键单击该文件并选择"运行自定义工具"

6.通过双击生成的.VSIX文件安装该工具 成功测试后,尝试安装可在"projectName/bin/debug"位置找到的VSIX.双击文件安装VSIX,按照安装步骤操作.现在您的工具可用于VS2017.使用工具类似,右键单击要运行自定义工具的文件,然后选择"运行自定义工具"

如果要卸载扩展,请转到"工具 - >扩展和更新 - >选择扩展",然后单击"卸载".请注意,在VS关闭之前,不会卸载工具.关闭后,您将获得一个要卸载的弹出窗口,选择"修改"进行卸载.

  • 这些说明也适用于 Visual Studio 2019。 (2认同)