Add References and Set a License - C# .NET Core

This tutorial shows how to get started with the LEADTOOLS SDK in a C# .NET Core application. Before any functionality from the SDK can be leveraged, a valid runtime license will have to be set. For instructions on how to obtain a runtime license refer to Obtaining a License.

Overview  
Summary This tutorial covers how to set a license in a C# .NET Core application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (0 KB)
Platform Windows C# Console Application
IDE Visual Studio 2017, 2019
Runtime License Download LEADTOOLS

Create the Project

Launch Visual Studio and select Create a new project, then select Next.

Creating new Visual Studio project

Select to create a new .NET Core Console application, then select Next.

Creating new Console App .NET Core

Add the project name and select the location for the project to be saved to, then select Create.

Adding Visual Studio project name and location

Add LEADTOOLS References via NuGet

To add the LEADTOOLS references in this tutorial, add a LEADTOOLS NuGet package since this will be using .NET Core.

Right-click on the C# project in the Solution Explorer and select Manage NuGet Packages...

Selecting Manage NuGet Packages

Browse for LEADTOOLS, then select any of the LEADTOOLS NuGet packages and install it. You will need to accept LEAD's End User License Agreement.

Installing LEADTOOLS NuGet packages

Add the Set License Code

Now that the LEADTOOLS references have been added to the project, add the relevant code to set your license.

Open the Program.cs in the Solution Explorer, then add using Leadtools; to the using block at the top.

In the Program class add a new method called SetLicense() and call it in the Main method. Add the below code to properly set your license.

C#
using System; 
using Leadtools; 
 
namespace add_references_and_set_a_license_tutorial 
{ 
   class Program 
   { 
      static void Main(string[] args) 
      { 
         if (!SetLicense()) 
            Console.WriteLine("Error setting license"); 
         else 
            Console.WriteLine("License file set successfully"); 
      } 
 
      static bool SetLicense() 
      { 
         if (RasterSupport.KernelExpired) 
         { 
            try 
            { 
               // TODO: Replace this with your License File contents 
               string licString = "[License]\n" + "License = <doc><ver>2.0</ver>`PASTE YOUR LICENSE CONTENTS HERE`</doc>"; 
               string developerKey = "PASTE YOUR DEVELOPER KEY HERE"; 
               byte[] licBytes = System.Text.Encoding.UTF8.GetBytes(licString); 
               RasterSupport.SetLicense(licBytes, developerKey); 
            } 
            catch (Exception ex) 
            { 
               Console.WriteLine(ex.Message); 
            } 
         } 
         return !RasterSupport.KernelExpired; 
      } 
   } 
} 

Run the Project

The project can then be built and run through Visual Studio or the command line provided the deployment environment has .NET Core supported and installed. To install .NET Core, refer to the installation guide on Microsoft's documentation site.

Windows

Run the project by pressing F5 or by selecting Debug then Start Debugging.

If the steps were followed correctly, a message will appear in the console showing License file set successfully.

Viewing console message that indicates license is set successfully

Mac and Linux

Open the terminal and browse to the project directory. Then execute dotnet build to build the project and dotnet run to execute it. Other commands include dotnet clean, dotnet rebuild, and dotnet restore.

On Mac:

Mac viewing terminal that indicates license is set successfully

On Linux:

Linux viewing terminal message that indicates license is set successfully

Wrap-up

This tutorial covered how to create a new C# .NET Core Project, how to add references via NuGet, and how to execute the project on Windows, Mac and Linux.

This is the basis for all C# .NET Core applications leveraging the LEADTOOLS SDK. All functionality in the SDK is unlocked via setting a license. The SetLicense method must be called before calling any other LEADTOOLS SDK functions.

Once the SDK is purchased, the evaluation license can be replaced with a valid runtime license to disable the Nag Message.

See Also

Help Version 20.0.2020.6.16
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2020 LEAD Technologies, Inc. All Rights Reserved.