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

This tutorial shows how to get started with the LEADTOOLS SDK in a C# .NET 6 Console application.

Overview  
Summary This tutorial covers how to set a license in a C# .NET 6 Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or higher
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

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.

Create the Project

Launch Visual Studio and select Create a new project.

Creating new Visual Studio project

Select Console App and click Next.

Creating new Console App .NET

Add the project name and specify the location where to save the project, then click Next.

Adding Visual Studio project name and location

Select the framework version, in this case .NET 6.0, and click Create.

Setting framework version to .NET 6.0

Add LEADTOOLS References

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both):

NuGet Reference - Add LEADTOOLS References via NuGet

To add the LEADTOOLS references via NuGet, perform the following steps:

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

Selecting Manage NuGet Packages

Under the Browse tab search for LEADTOOLS, for the purpose of this tutorial any of the NuGet packages will work. LEAD's End User License Agreement will need to be accepted at this time.

Installing LEADTOOLS NuGet packages

Local Reference - Add LEADTOOLS References Locally via DLL

Right-click on Dependencies in the Solution Explorer and click Add Project Reference....

Adding Project Reference

In the Reference Manager dialog box, select Browse and then navigate to the directory where the LEADTOOLS Assemblies are located: <INSTALL_DIR>\LEADTOOLS23\Bin\net

Select and add Leadtools.dll and Leadtools.Core.dll, then click OK.

Selecting Leadtools.dll to add

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 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 InitLEAD() and call it in the Main method. Add the below code to properly set the LEADTOOLS native runtimes and your license.

C#
using System; 
using Leadtools; 
 
namespace add_references_and_set_a_license_tutorial 
{ 
   class Program 
   { 
      static void Main(string[] args) 
      { 
         InitLEAD(); 
      } 
      static void InitLEAD() 
      { 
         var leadSdkBase = @"C:\LEADTOOLS23\"; 
         var nativeRuntimes = Path.Combine(leadSdkBase, @"Bin\CDLL\x64\"); 
         var licenseDir = Path.Combine(leadSdkBase, @"Support\Common\License\"); 
 
         // Set the library path to the native runtime location 
         Platform.LibraryPath = nativeRuntimes; 
 
         // Set the LEADTOOLS License 
         RasterSupport.SetLicense(licenseDir + "LEADTOOLS.LIC", File.ReadAllText(licenseDir + "LEADTOOLS.LIC.KEY")); 
 
         if (RasterSupport.KernelExpired) 
            Console.WriteLine("License file invalid or expired."); 
         else 
            Console.WriteLine("License file set successfully."); 
      } 
   } 
} 

Note

When using LEADTOOLS with .NET 6+, set the native runtimes using the LibraryPath property.

Run the Project

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

Windows

Run the project by pressing F5, or by selecting Debug -> 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 Project, how to add LEADTOOLS references, and how to execute the project on Windows, Mac and Linux.

This is the basis for all C# .NET 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 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.