CSharp and OBDotNet#

OBDotNet is a compiled assembly that allows Open Babel to be used from the various .NET languages (e.g. Visual Basic, C#, IronPython, IronRuby, and J#) on Windows, Linux and MacOSX. The current version is OBDotNet 0.4.



The OBDotNet.dll assembly provided on Windows was compiled using the .NET framework v3.5 for the x86 platform. To use it, you will need to compile your code using .NET v3.5 or newer and you will also need to target x86 (/platform:x86).

The following instructions describe how to compile a simple C# program that uses OBDotNet:

  1. First you need to download and install the OpenBabelGUI version 2.3.2

  2. Next create an example CSharp program that uses the Open Babel API (see below for one or use this link). Let’s call this example.cs.

  3. Copy OBDotNet.dll from the Open Babel installation into the same folder as example.cs.

  4. Open a command prompt at the location of example.cs and compile it as follows:

    C:\Work> C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe
                               /reference:OBDotNet.dll /platform:x86 example.cs
  5. Run the created executable, example.exe, to discover the molecule weight of propane:

    C:\Work> example.exe

If you prefer to use the MSVC# GUI, note that the Express edition does not have the option to choose x86 as a target. This will be a problem if you are using a 64-bit operating system. There’s some information at Coffee Driven Development on how to get around this.

MacOSX and Linux#

On Linux and MacOSX you need to use Mono, the open source implementation of the .NET framework, to compile the bindings. The following instructions describe how to compile and use these bindings:

  1. OBDotNet.dll is included in the Open Babel source distribution in scripts/csharp. To compile a CSharp application that uses this (e.g. the example program shown below), use a command similar to the following:

    gmcs example.cs /reference:../openbabel-2.3.2/scripts/csharp/OBDotNet.dll
  2. To run this on MacOSX or Linux you need to compile the CSharp bindings as described in the section Compile language bindings. This creates lib/libopenbabel_csharp.so in the build directory.

  3. Add the location of OBDotNet.dll to the environment variable MONO_PATH. Add the location of libopenbabel_csharp.so to the environment variable LD_LIBRARY_PATH. Additionally, if you have not installed Open Babel globally you should set BABEL_LIBDIR to the location of the Open Babel library and BABEL_DATADIR to the data directory.

  4. Run example.exe:

    $ ./example.exe


The API is almost identical to the Open Babel C++ API. Differences are described here.

Using iterators

In OBDotNet, iterators are provided as methods of the relevant class. The full list is as follows:

Such iterators are used as follows:

foreach (OBAtom atom in myobmol.Atoms())

Other iterators in the C++ API not listed above can still be used through their IEnumerator methods.

Handling OBGenericData

To cast :obapi:`OBGenericData` to a specific subclass, you should use the .Downcast <T> method, where T is a subclass of OBGenericData.

Open Babel Constants

Open Babel constants are available in the class openbabelcsharp.


The following sections show how the same example application would be programmed in C#, Visual Basic and IronPython. The programs print out the molecular weight of propane (represented by the SMILES string “CCC”).


using System;
using OpenBabel;

namespace MyConsoleApplication
    class Program
        static void Main(string[] args)
            OBConversion obconv = new OBConversion();
            OBMol mol = new OBMol();
            obconv.ReadString(mol, "CCC");

Visual Basic

Imports OpenBabel

Module Module1

    Sub Main()
        Dim OBConv As New OBConversion()
        Dim Mol As New OBMol()

        OBConv.ReadString(Mol, "CCC")
        System.Console.Write("The molecular weight of propane is " & Mol.GetMolWt())
    End Sub

End Module


import clr

import OpenBabel as ob

conv = ob.OBConversion()
mol = ob.OBMol()
conv.ReadString(mol, "CCC")