VsTools.Projects 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package VsTools.Projects --version 1.0.0                
NuGet\Install-Package VsTools.Projects -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="VsTools.Projects" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VsTools.Projects --version 1.0.0                
#r "nuget: VsTools.Projects, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install VsTools.Projects as a Cake Addin
#addin nuget:?package=VsTools.Projects&version=1.0.0

// Install VsTools.Projects as a Cake Tool
#tool nuget:?package=VsTools.Projects&version=1.0.0                

Introduction

VsTools.Projects is a library for reading and modifying the contents of (currently) Visual Studio C# Project (.csproj) files.

It grew out of a need to automate the insertion of newly created files using data extracted from existing files.

NOTE: This is not supposed to be used inside Visual Studio as an alternative to writing an Visual Studio extension. Rather, it is a way to automate modifying the csproj XML easily, without having to think about elements and attributes and indentation.

Features

  • Read and write .csproj files (Only tested on 2017)
  • Access to first-level elements easily as IEnumerable properties
    • Import
    • PropertyGroup
    • ItemGroup
    • Target
  • Add and Remove Elements
  • Default attributes and text elements exposed as properties, just get and set
  • Automatic indentation

Usage

Add an ItemGroup with a Compile element


// Open a project, create a new ItemGroup and add some files that will be nested under existing files
// in the project, assuming that the file resides in the same folder as the dependency
// and add the new ItemGroup after the first ItemGroup

var proj = Project.Load("path\\to\\project.csproj");

var firstItemGroup = proj.ItemGroups.First();

var itemGroup = new ItemGroup();

var newFile = new Compile("path\\to\\file.cs");

newFile.DependentUpon = "parent.cs";

itemGroup.AddContent(newfile);

firstItemGroup.AddAfterSelf(itemGroup);

project.Save("path\\to\\project.csproj");

This will insert the following xml after the first item group.

  <ItemGroup>
    <Compile Include="path\to\file.cs">
      <DependentUpon>parent.cs</DependentUpon>
    </Compile>
  </ItemGroup>

Access PropertyGroup Elements

PropertyGroup child elements can be accessed through the object's indexer as elements vary between PropertyGroups. You can get or set an element through the accessor. Setting an element that does not exist will create the element. Setting the value on an existing element to null will remove the element.


// Open a project, create a new ItemGroup and add some files that will be nested under existing files
// in the project, assuming that the file resides in the same folder as the dependency
// and add the new ItemGroup after the first ItemGroup

var proj = Project.Load("path\\to\\project.csproj");

var debug = proj.PropertyGroups.First(x => x.Condition == " '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ");

Console.WriteLine(debug["DebugSymbols"]);
Console.WriteLine(debug["DebugType"]);
Console.WriteLine(debug["Optimize"]);
Console.WriteLine(debug["OutputPath"]);
Console.WriteLine(debug["DefineConstants"]);
Console.WriteLine(debug["ErrorReport"]);
Console.WriteLine(debug["WarningLevel"]);
Console.WriteLine(debug["Prefer32Bit"]);
Console.WriteLine(debug["PrecompileBeforePublish"]);
Console.WriteLine(debug["RunCodeAnalysis"]);
Console.WriteLine(debug["TreatWarningsAsErrors"]);
Console.WriteLine(debug["PublishDatabases"]);
Console.WriteLine(debug["CodeAnalysisAdditionalOptions"]);

This will output:

true
full
false
bin\
DEBUG;TRACE
prompt
4
false
false
false
false
false
/assemblyCompareMode:StrongNameIgnoringVersion

ItemGroup Contents

Since ItemGroup Contents are fairly consistent, there are specialized classes for accessing them.

Example ItemGroup Contents:

  <ItemGroup>
    <Reference Include="Binbin.Linq.PredicateBuilder, Version=1.0.3.26645, Culture=neutral, processorArchitecture=MSIL">
      <HintPath>..\packages\Binbin.Linq.PredicateBuilder.1.0.3.26645\lib\net45\Binbin.Linq.PredicateBuilder.dll</HintPath>
      <Private>True</Private>
    </Reference>
    <Compile Include="path\to\class.cs"/>
      <DependentUpon>page.cshtml</DependentUpon>
    </Compile/>
    <Content Include="path\to\page.cshtml" />
  <ItemGroup>

ItemGroup contents inherit from ItemGroupContent. Accessing the Contents property will return a list of typed objects depending on the class name.

var contents = itemGroup.Contents.ToList();

// of type Reference
Console.WriteLine(contents[0].HintPath);

// of type Compile
Console.WriteLine(contents[1].DependentUpon);

// of type Content
Console.WriteLine(contents[0].Include);

Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on VsTools.Projects:

Package Downloads
HybridCLR.QuickRun.Client

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 1,332 1/6/2021
1.0.0 1,087 12/1/2017