MSBuildProjectModifier 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package MSBuildProjectModifier --version 1.0.0                
NuGet\Install-Package MSBuildProjectModifier -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="MSBuildProjectModifier" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MSBuildProjectModifier --version 1.0.0                
#r "nuget: MSBuildProjectModifier, 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 MSBuildProjectModifier as a Cake Addin
#addin nuget:?package=MSBuildProjectModifier&version=1.0.0

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

**MSBuildProjectModifier** is a .NET library that simplifies adding and managing file references in MSBuild project files. The library provides functionality to programmatically add file references, check if references exist, and verify if project files are present.

## Features
- Add references to files in MSBuild project files.
- Check if a reference to a file already exists in the project.
- Verify if an MSBuild project file exists.

## Installation

Install the NuGet package via the .NET CLI:

```bash
dotnet add package MSBuildProjectModifier

Or use the NuGet Package Manager in Visual Studio:

Install-Package MSBuildProjectModifier

Usage

1. Add a File Reference to an MSBuild Project

You can use the AddReference method to add a file reference to an MSBuild project. If the reference already exists, no changes are made.

using MSBuildProjectModifier;
using Microsoft.Build.Evaluation;

public class Example
{
    private readonly IMsBuildManager _msBuildManager;

    public Example(IMsBuildManager msBuildManager)
    {
        _msBuildManager = msBuildManager;
    }

    public void AddReferenceToProject()
    {
        string projectFilePath = @"path\to\your\project.csproj";
        string filePath = @"path\to\your\file.config";
        string itemType = "Content";
        ProjectLoadSettings loadSettings = ProjectLoadSettings.Default;

        _msBuildManager.AddReference(projectFilePath, filePath, itemType, loadSettings);
    }
}

2. Check if a Reference Exists

You can use the IsReferenceExists method to check if a reference to a specific file exists in the MSBuild project file.

public bool CheckReferenceExists()
{
    string projectFilePath = @"path\to\your\project.csproj";
    string filePath = @"path\to\your\file.config";
    string attribute = "Include";
    LoadOptions loadOptions = LoadOptions.None;

    return _msBuildManager.IsReferenceExists(projectFilePath, filePath, attribute, loadOptions);
}

3. Check if a Project File Exists

You can use the IsProjectExists method to check if the given MSBuild project file exists.

public bool CheckProjectExists()
{
    string projectFilePath = @"path\to\your\project.csproj";
    return _msBuildManager.IsProjectExists(projectFilePath);
}

API

AddReference(string projectFilePath, string filePath, string itemType, ProjectLoadSettings projectLoadSettings)

  • projectFilePath: Full path to the MSBuild project file.
  • filePath: Full path to the file you want to add as a reference.
  • itemType: The type of item to add (e.g., Content, Reference).
  • projectLoadSettings: Settings used to load the MSBuild project.

IsReferenceExists(string projectFilePath, string filePath, string attribute, LoadOptions loadOptions)

  • projectFilePath: Full path to the MSBuild project file.
  • filePath: Full path to the file reference to check.
  • attribute: Attribute to check (e.g., Include).
  • loadOptions: XML load options for parsing the project file.

IsProjectExists(string projectFilePath)

  • projectFilePath: Full path to the MSBuild project file.
  • Returns: True if the project file exists, otherwise False.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 93 9/25/2024
1.0.0 84 9/24/2024