我有一個Assembly,其中包含BuildAction = Content和Copy to output = Always的文件。現在我構建依賴於Assembly的Executable,並且我希望VisualStudio/MsBuild以某種方式複製內容文件以便輸出,以便Assembly能夠工作。但它沒有發生。BuildAction =內容遞歸
目前,我手動解決此問題:通過將這些文件添加爲可執行文件項目的鏈接或在構建事件中進行復制。有沒有辦法以自動的方式解決這個問題?
我有一個Assembly,其中包含BuildAction = Content和Copy to output = Always的文件。現在我構建依賴於Assembly的Executable,並且我希望VisualStudio/MsBuild以某種方式複製內容文件以便輸出,以便Assembly能夠工作。但它沒有發生。BuildAction =內容遞歸
目前,我手動解決此問題:通過將這些文件添加爲可執行文件項目的鏈接或在構建事件中進行復制。有沒有辦法以自動的方式解決這個問題?
如果您手動編輯代表項目的.csproj文件,則可以完成此操作。例如,假設您有一個名爲「二進制文件」的文件夾,您需要將其複製到輸出目錄(我建議使用None的構建操作並始終複製到輸出目錄,但此方法可以適用於任一種,正如我將在下面顯示)。:
關閉visual studio,沒有任何「二進制文件」文件或文件夾添加。在Windows資源管理器中,瀏覽到您的項目目錄,並手動創建這個「二進制文件」文件夾和所需的文件。然後,使用文本編輯器編輯項目的.csproj文件。用幾個「ItemGroup」標籤查找文件的一部分。你會想使這個此外:當你再次打開Visual Studio中的項目
<ItemGroup>
<None Include="binaries\**\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
,你會發現你的包裹用正確的生成操作和複印設置。
同樣可以與內容生成操作來完成,而不是:
<ItemGroup>
<Content Include="binaries\**\*.*" />
</ItemGroup>
不過,我已經在野外使用此卻不太順利。
這是另一種答案,解決這個問題:
Is there a way to automatically include content files into asp.net project file?
//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...
您可以將BuildAction的改變EmbeddedResource,這樣它的大會中,你可以很容易地訪問它。
是否絕對有必要保持BuildAction = Content? – MAXE 2012-08-28 07:42:56
@MAXE,我不確定...這是什麼想法? – SiberianGuy 2012-08-28 07:46:58
嘗試清理整個解決方案並重建所有內容:我嘗試在我的機器上運行,但沒有問題(在WPF解決方案中)。這可能在MSDN上:查看http://msdn.microsoft.com/en-us/library/aa970494(v=vs.100).aspx#Content_Files的「內容文件」部分 – MAXE 2012-08-28 07:54:21