Is it possible to automatically set “Copy to Output Directory” when creating files in Visual Studio 2010?

前端 未结 1 1563
攒了一身酷
攒了一身酷 2021-01-04 20:49

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 instal

相关标签:
1条回答
  • 2021-01-04 21:25

    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);
            }    
        }
    
    0 讨论(0)
提交回复
热议问题