Is it possible to automatically set “Copy to Outpu

2019-02-13 17:07发布

问题:

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.

回答1:

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.

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..

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