在使用Visual Studio 2010创建文件时,是否可能自动设置“复制到输出目录”?

9
我最近开始使用LuaInterface在我的C#程序中实现Lua脚本。为了方便地在Visual Studio中创建Lua脚本,我安装了一个Lua语法高亮插件并创建了一个项目模板,以便我可以右键单击项目文件并选择“New Item-> Lua Script”来创建新的脚本。这个很好用。
为了让程序找到这些脚本,它们需要放置在生成位置的同一目录(或子目录)中。这正是我想要的,但是为了做到这一点,我必须为每个新文件更改“复制到输出目录”设置。
有没有一种方法可以更改此选项的默认设置?现在它默认为“不复制”。我只能找到另一个问题与我的问题类似,但那里提供的唯一答案建议使用后期构建事件将所有具有相同扩展名的文件复制到定义的位置。我不希望这样做,因为目标位置可能会更改或添加更多目标(需要额外的事件?),而且我希望能够按文件更改该设置。
这只是一个方便问题,因为我可以手动更改每个文件的选项,但是既然已经能够自动化其余过程,我希望也能自动化这个最后的细节。
1个回答

4
你应该能够向模板添加一个IWizard引用,这将在单击文件- > 添加窗口中的ok时运行。你需要在vstemplate文件中添加程序集和类型
实现RunFinished或可能是ProjectItemFinishedGenerating方法。然后,你可以使用Visual Studio公开的EnvDTE对象,使用标准的Visual Studio可扩展性模型来操作解决方案中的任何项目。 以下代码片段(来自开源T4工具箱)显示了如何设置此属性。
    /// <summary>
    /// Sets the known properties for the <see cref="ProjectItem"/> to be added to solution.
    /// </summary>
    /// <param name="projectItem">
    /// A <see cref="ProjectItem"/> that represents the generated item in the solution.
    /// </param>        
    /// <param name="output">
    /// An <see cref="OutputFile"/> that holds metadata about the <see cref="ProjectItem"/> to be added to the solution.
    /// </param>
    private static void SetProjectItemProperties(ProjectItem projectItem, OutputFile output)
    {
        // Set "Build Action" property
        if (!string.IsNullOrEmpty(output.BuildAction))
        {
            ICollection<string> buildActions = GetAvailableBuildActions(projectItem);               
            if (!buildActions.Contains(output.BuildAction))
            {
                throw new TransformationException(
                    string.Format(CultureInfo.CurrentCulture, "Build Action {0} is not supported for {1}", output.BuildAction, projectItem.Name));
            }

            SetPropertyValue(projectItem, "ItemType", output.BuildAction);
        }

        // Set "Copy to Output Directory" property
        if (output.CopyToOutputDirectory != default(CopyToOutputDirectory))
        {
            SetPropertyValue(projectItem, "CopyToOutputDirectory", (int)output.CopyToOutputDirectory);
        }

        // Set "Custom Tool" property
        if (!string.IsNullOrEmpty(output.CustomTool))
        {
            SetPropertyValue(projectItem, "CustomTool", output.CustomTool);
        }

        // Set "Custom Tool Namespace" property
        if (!string.IsNullOrEmpty(output.CustomToolNamespace))
        {
            SetPropertyValue(projectItem, "CustomToolNamespace", output.CustomToolNamespace);
        }    
    }

谢谢!这似乎可能有效,但您是否有示例或教程的链接?我正在尝试按照http://msdn.microsoft.com/en-us/library/ms185301.aspx上的教程进行操作,但我发现有点难以处理。我也很难找到控制“复制到输出目录”设置的选项。您提到了EnvDTE对象,所以我认为它包含在EnvDTE.ProjectItem.Properties集合中。这是我第一次尝试编写编辑器扩展,而且似乎这些东西的文档有点稀少。 - Shaun Hamman
工作得非常好。那个代码示例帮了大忙。谢谢! - Shaun Hamman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接