2011-06-21 41 views
1

我在寫一個使用SQLite和Excel的C#程序。自然,我需要System.Data.SQLite.dll和Microsoft.Office.Interop.Excel.dll。我希望我的程序是一個單獨的exe文件,所以我想要將這些DLL包裝在程序集中。這是我的.csproj文件:捆綁Microsoft.Office.Interop.Excel.dll作爲資源不能正常工作

<Project DefaultTargets="Compile" 
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 

<PropertyGroup> 
    <appname>AppName</appname> 
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> 
</PropertyGroup> 

<ItemGroup> 
    <CSFile Include="*.cs"/> 
</ItemGroup> 

<ItemGroup> 
    <DLLResource Include="System.Data.SQLite.dll"/> 
    <DLLResource Include="Microsoft.Office.Interop.Excel.dll"/> 

    <DLLReference Include="System.Data.SQLite.dll"/> 
    <DLLReference Include="Microsoft.Office.Interop.Excel.dll"/> 
</ItemGroup> 

<ItemGroup> 
    <BundledResource Include="std_db_build.xml"/> 
    <BundledResource Include="std_report_make.xml"/> 
</ItemGroup> 

<Target Name="Compile"> 
    <CSC 
      Sources="@(CSFile)" 
      Resources="@(DLLResource);@(BundledResource)" 
      References="@(DLLReference)" 
      OutputAssembly="$(appname).exe"> 
    </CSC> 
</Target> 

<Target Name="Run"> 
    <Exec Command="start $(COMSPEC) /k &quot;$(PathTo)$(appname).exe &amp; pause>null &amp; exit&quot;" /> 
</Target> 

<Target Name="Clean"> 
    <Delete Files="$(appname).exe"/> 
</Target> 
</Project> 

現在,這是System.Data.SQLite.dll做工精細 - 建設程序後,我的程序不需要DLL是在同一個文件夾中工作。然而,的可執行文件要求將Microsoft.Office.Interop.Excel.dll放在同一個文件夾中,儘管我捆綁它就像捆綁System.Data.SQLite.dll一樣。

現在,我知道Microsot.Office.Interop.Excel.dll是在程序集內部,因爲它將文件大小從928k增加到1952k - 增加了1024k,這正好是Microseft.Office.Interop的大小.Excel.dll。所以我假設即使DLL在程序集內部,程序的某些部分仍然會將其作爲單獨的文件進行查找。

那麼,我做錯了什麼?我該如何解決它?

回答

0

可以消除對Interop程序集的需求。這可能是你最好的選擇。

這對MSDN上的article應該有所幫助。

總之:

選擇Interop庫的參考。在「屬性」窗口中,確保Embed Interop Types屬性設置爲True。

+0

謝謝,但我沒有使用VS,我不知道如何在MSBuild項目文件中做到這一點... –

+0

它看起來像這是.NET 4.0的語言功能,你使用的是3.5 。除非您有升級選項,否則此方法不適用於您。 –