Search code examples
c#c#-4.0msbuildcompilationcsproj

How do I include DLLs in my .csproj file?


Well, the thing is that I don't have Visual Studio installed and I don't want to install it, so, I made a batch file that compiles my .csproj file and all of my source files too.

The problem is that I don't know how to include .dll files. Here is my current code for my .csproj file:

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <AssemblyName>Project</AssemblyName>
    <OutputPath>Bin\</OutputPath>
  </PropertyGroup>

  <ItemGroup>
        <!-- .cs files -->
    <Compile Include="program.cs" />
  </ItemGroup>

  <Target Name="Build">
    <MakeDir Directories="$(OutputPath)"      Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
  </Target>
</Project>

What do I need to change to include / reference a dll file into the compilation process?


Solution

  • You will need an ItemGroup with items called Reference like so:

    <ItemGroup>
        <Reference Include="Microsoft.Practices.Unity" />
        <Reference Include="MvcMiniProfiler">
          <HintPath>..\packages\MiniProfiler.1.6\lib\MvcMiniProfiler.dll</HintPath>
        </Reference>
        <Reference Include="System" />
        <Reference Include="System.configuration" />
        <Reference Include="System.Core" />
        <Reference Include="System.Data.Entity" />
        <Reference Include="System.Runtime.Serialization" />
        <Reference Include="System.Security" />
        <Reference Include="System.Web" />
        <Reference Include="System.Xml.Linq" />
        <Reference Include="System.Data.DataSetExtensions" />
        <Reference Include="Microsoft.CSharp" />
        <Reference Include="System.Data" />
        <Reference Include="System.Xml" />
      </ItemGroup>
    

    If you are referencing non-GAC'd dlls, you will need to either put in the HintPath (see mvc mini profiler, this should be relative to your build files location), or you will need to pass the path to MSBuild in its ReferencePath property.