是否可以自动设置“复制到输出目录"?在 Visual Studio 2010 中创建文件时? [英] Is it possible to automatically set "Copy to Output Directory" when creating files in Visual Studio 2010?

查看:25
本文介绍了是否可以自动设置“复制到输出目录"?在 Visual Studio 2010 中创建文件时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently started playing around with LuaInterface to get Lua scripting working in my C# programs. In order to easily create Lua scripts from within Visual Studio, I installed a Lua syntax highlighting plugin and created an Item Template so that I could create new scripts by right clicking on the project file and selecting "New Item->Lua Script". This works quite well.

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

In order for the program to find the scripts, they need to be placed in the same directory (or a subdirectory) of the build location. This is exactly where I want them, but in order to do this, I have to change the "Copy to Output Directory" setting for each new file I create.

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

Is there a way to change the default setting of this option? Right now it defaults to "Do not copy". I could only find one other question asking essentially the same thing but the only answer provided there suggested a post-build event copying all files with the same extension to a defined location. I don't really want to do this since the target destination may change or more targets may be added (and would require additional events?) and I would like to be able to change that setting on a per-file basis.

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

This is just a convenience issue, as I can change that option manually for each file, but having been able to automate the rest of the process, I was hoping I could automate this one last detail as well.

推荐答案

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

You should be able to add an IWizard reference to the template, this will run when you click ok in the File -> Add window. You'll need to add the assembly and type to the vstemplate file.

实施RunFinished 或者可能是 ProjectItemFinishedGenerating 方法.然后,您可以使用 Visual Studio 公开的 EnvDTE 对象来操作使用标准 Visual Studio 扩展模型的解决方案中的任何项目.

Implement the RunFinished or possibly the ProjectItemFinishedGenerating method. You can then use the EnvDTE object exposed by Visual Studio to manipulate any item in the solution using the standard Visual Studio Extensibility model..

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

The following code snippit (from the open source T4 Toolbox) shows how to set this property.

    /// <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);
        }    
    }

这篇关于是否可以自动设置“复制到输出目录"?在 Visual Studio 2010 中创建文件时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆