【问题标题】:BuildAction=Content recursivelyBuildAction=递归内容
【发布时间】:2012-07-26 17:59:53
【问题描述】:

我有一个包含 BuildAction = Content 和 Copy to output = Always 的文件的程序集。现在我构建依赖于 Assembly 的 Executable,我希望 VisualStudio/MsBuild 以某种方式将内容文件复制到输出,以便 Assembly 可以工作。但它不会发生。

目前我手动解决了这个问题:要么通过将这些文件添加为可执行项目的链接,要么在构建事件中复制。有没有办法自动解决这个问题?

【问题讨论】:

  • 是否绝对有必要保持 BuildAction = Content?
  • @MAXE,我不确定...这是什么想法?
  • 尝试清理整个解决方案并重新构建所有解决方案:我在我的机器上尝试过,它可以正常工作(在 WPF 解决方案中)。在 MSDN 上是可能的:查看 msdn.microsoft.com/en-us/library/… 的“内容文件”部分

标签: .net msbuild


【解决方案1】:

如果您手动编辑代表项目的 .csproj 文件,则可以完成此操作。

例如,假设您有一个名为“binaries”的文件夹,您需要将其复制到输出目录(我建议使用无构建操作并始终复制到输出目录,但此方法适用于任何一种,因为我会在下面展示)。:

关闭 Visual Studio,不添加任何“二进制”文件或文件夹。在 Windows 资源管理器中,浏览到您的项目目录,并手动创建此“二进制”文件夹,其中包含所需的文件。然后,使用文本编辑器编辑项目的 .csproj 文件。查找包含多个“ItemGroup”标签的文件部分。您需要添加以下内容:

<ItemGroup>
    <None Include="binaries\**\*.*">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
</ItemGroup>

当您再次在 Visual Studio 中打开项目时,您会发现包含正确的构建操作和复制设置。

内容构建操作也可以这样做:

<ItemGroup>
    <Content Include="binaries\**\*.*" />
</ItemGroup>

但是,我在野外使用它的成功率较低。

这是解决此问题的另一个答案:

Is there a way to automatically include content files into asp.net project file?

【讨论】:

    【解决方案2】:
    //this is the class library project
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ClassLibrary1
    {
        public class Class1
        {
            public Class1()
            {
                string res = "";
                using (StreamReader sr = new StreamReader("TextFile1.txt"))
                {
                    res = sr.ReadToEnd();
                }
                Console.WriteLine("ok dll {0}",res);
                Console.ReadLine();
            }
        }
    }
    //I add a text file which name is TextFile1.txt with BuildAction = Content and Copy to output = //Always 
    //it contains a sentence
    i am in the file no problem
    
    
    //a console application that refers the class library
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.SqlClient;
    using System.Data;
    using ClassLibrary1;
    
    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
            {
                Class1 c = new Class1();
    
            }
        }
    }
    
    
    //it is ok, with a rebuild all if you update the text file...
    

    【讨论】:

      【解决方案3】:

      您可以将 BuildAction 更改为 EmbeddedResource,这样它就在程序集中并且您可以轻松访问它。

      【讨论】:

        猜你喜欢
        • 2013-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-28
        • 2011-05-31
        • 2021-07-23
        相关资源
        最近更新 更多