在Visual Studio 2010中创建文件时是否可以自动设置"复制到输出目录"?

Sha*_*man 8 automation properties visual-studio-2010 visual-studio-sdk

我最近开始使用LuaInterface来让Lua脚本在我的C#程序中运行.为了从Visual Studio中轻松创建Lua脚本,我安装了一个Lua语法高亮插件并创建了一个项模板,这样我就可以通过右键单击项目文件并选择"New Item-> Lua Script"来创建新脚本.这非常有效.

为了让程序找到脚本,需要将它们放在构建位置的同一目录(或子目录)中.这正是我想要的地方,但为了做到这一点,我必须为我创建的每个新文件更改"复制到输出目录"设置.

有没有办法更改此选项的默认设置?现在它默认为"不要复制".我只能找到另一个问题基本上是同一个问题,但是那里提供的唯一答案是建议将构建后事件复制到具有相同扩展名的所有文件到定义的位置.我真的不想这样做,因为目标目的地可能会改变或者可能会添加更多目标(并且需要额外的事件?),我希望能够在每个文件的基础上更改该设置.

这只是一个方便的问题,因为我可以手动为每个文件更改该选项,但是已经能够自动执行其余的过程,我希望我也可以自动完成最后一个细节.

jes*_*ing 4

您应该能够添加IWizard对模板的引用,这将在您单击ok“文件”->“添加”窗口时运行。您需要将程序集和类型添加到 vstemplate 文件中。

实现该方法RunFinished或可能实现该ProjectItemFinishedGenerating方法。然后,您可以使用EnvDTEVisual Studio 公开的对象,通过标准 Visual Studio 可扩展性模型来操作解决方案中的任何项目。

以下代码片段(来自开源 T4 Toolbox)显示了如何设置此属性。

    /// <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);
        }    
    }
Run Code Online (Sandbox Code Playgroud)