LdifHelper 2.2.0

dotnet add package LdifHelper --version 2.2.0
NuGet\Install-Package LdifHelper -Version 2.2.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="LdifHelper" Version="2.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add LdifHelper --version 2.2.0
#r "nuget: LdifHelper, 2.2.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 LdifHelper as a Cake Addin
#addin nuget:?package=LdifHelper&version=2.2.0

// Install LdifHelper as a Cake Tool
#tool nuget:?package=LdifHelper&version=2.2.0

LdifHelper

NuGet License

A library for reading and writing RFC 2849 LDIF files.

Usage

Reading

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

using LdifHelper;

internal class Program
{
    /// <summary>
    /// Pattern splits a distinguished name on the first unescaped comma.
    /// </summary>
    private static readonly Regex DistinguishedNameRegex =
        new(@"\s*(?<=[^\\]),\s*(?=\w+\s*=\s*)", RegexOptions.Compiled, TimeSpan.FromSeconds(1));

    private static void Main()
    {
        // Open and parse an ASCII encoded file. Use 1252 when reading Microsoft's ldifde.exe output.
        using var streamReader = new StreamReader("input.ldif", Encoding.GetEncoding(20127), false);

        foreach (var changeRecord in LdifReader.Parse(streamReader))
        {
            if (changeRecord is ChangeAdd changeAdd)
            {
                Console.WriteLine($"Adding {changeAdd.DistinguishedName}");
                foreach (var attribute in changeAdd)
                {
                    Console.WriteLine($"  * {attribute.AttributeType}");
                    foreach (var value in attribute)
                    {
                        Console.WriteLine($"    - {value}");
                    }
                }

                continue;
            }

            if (changeRecord is ChangeDelete changeDelete)
            {
                Console.WriteLine($"Deleting {changeDelete.DistinguishedName}");

                continue;
            }

            if (changeRecord is ChangeModDn changeModDn)
            {
                var components = DistinguishedNameRegex.Split(changeModDn.DistinguishedName, 2);
                if (components.Length != 2)
                {
                    throw new InvalidOperationException(
                        $"Invalid distinguished name found for {changeModDn.DistinguishedName}.");
                }

                // Only the newsuperior field is optional.
                var newDistinguishedName =
                    changeModDn.NewSuperior is null
                        ? $"{changeModDn.NewRdn},{changeModDn.DistinguishedName.Substring(components[0].Length + 1)}"
                        : $"{changeModDn.NewRdn},{changeModDn.NewSuperior}";

                Console.WriteLine($"Renaming {changeModDn.DistinguishedName}");
                Console.WriteLine($"  * New DN: {newDistinguishedName}.");
                Console.WriteLine($"  * Delete old rdn: {changeModDn.DeleteOldRdn}.");

                continue;
            }

            if (changeRecord is ChangeModify changeModify)
            {
                Console.WriteLine($"Modifying {changeModify.DistinguishedName}");
                foreach (var modSpec in changeModify.ModSpecs)
                {
                    switch (modSpec.ModSpecType)
                    {
                        case ModSpecType.Add:
                            Console.WriteLine($"  * Adding the following values to {modSpec.AttributeType}:");
                            foreach (var value in modSpec)
                            {
                                Console.WriteLine($"    - {value}");
                            }

                            break;

                        case ModSpecType.Delete:
                            if (modSpec.Count == 0)
                            {
                                Console.WriteLine($"  * Deleting all values from {modSpec.AttributeType}.");
                            }
                            else
                            {
                                Console.WriteLine($"  * Deleting the following values from {modSpec.AttributeType}:");
                                foreach (var value in modSpec)
                                {
                                    Console.WriteLine($"    - {value}");
                                }
                            }

                            break;

                        case ModSpecType.Replace:
                            Console.WriteLine($"  * Replacing all values from {modSpec.AttributeType}:");
                            foreach (var value in modSpec)
                            {
                                Console.WriteLine($"    - {value}");
                            }

                            break;

                        default:
                            throw new InvalidOperationException(
                                $"Unknown mod-spec type: {modSpec.ModSpecType}.");
                    }
                }
            }
            else
            {
                throw new InvalidOperationException(
                    $"Unknown change record type: {changeRecord.GetType().BaseType}.");
            }
        }
    }
}

Writing

using System.Collections;
using System.Collections.Generic;
using System.DirectoryServices;
using System.IO;
using System.Text;
using LdifHelper;

internal class Program
{
    private static void Main()
    {
        using var fileStream = new FileStream("output.ldif", FileMode.Create);
        using var streamWriter = new StreamWriter(fileStream, Encoding.ASCII);
        using var directorySearcher = new DirectorySearcher();

        foreach (SearchResult searchResult in directorySearcher.FindAll())
        {
            var ldifAttributes = new List<LdifAttribute>();
            foreach (DictionaryEntry dictionaryEntry in searchResult.Properties)
            {
                var values = new List<object>();
                foreach (var value in (ResultPropertyValueCollection)dictionaryEntry.Value)
                {
                    /*
                     * The library does not make assumptions on what the string representation of an object should be.
                     * All types must be converted to either a string or byte[] before being boxed.
                     */
                    values.Add(value is byte[] ? value : value.ToString());
                }

                ldifAttributes.Add(new LdifAttribute((string)dictionaryEntry.Key, values));
            }

            var changeAdd = new ChangeAdd(searchResult.Path.Substring(7), ldifAttributes);
            streamWriter.WriteLine(changeAdd.Dump());
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

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
2.2.0 678 9/11/2023
2.1.2 2,770 1/23/2023
2.1.1 286 1/17/2023
2.1.0 511 7/10/2022
2.0.0 4,161 2/17/2018
1.0.2 1,351 9/14/2017
1.0.1 1,126 6/24/2017