OPC Studio User's Guide and Reference
OPC Studio .NET Examples
Examples > Examples in the Product and on the Web > OPC Studio .NET Examples
In This Topic

The .NET examples are currently provided for Microsoft Visual Studio 2022. Some examples can also be used with .NET 6 or 8 or .NET CLI Tools, JetBrain Rider, and Visual Studio Code. They are likely to work with other development tools as well, but we have not tested it.

The examples are targeting Microsoft .NET Framework 4.7.2 (and will also run under newer versions), and/or .NET 6 or 8. In Visual Studio, it is possible to re-target the projects to newer framework versions (e.g.  4.8) by changing the appropriate setting in the properties of the project, or by editing the project file manually.

On Windows, if you have used the Setup Program to install the OPC Studio, Visual Studio solutions with examples in Visual Basic (i.e. VB.NET), C#, F# and C++ are available from the Start menu or the Launcher application. On other platforms (or when you are simply installing the OPC Data Client or OPC Wizard NuGet Packages), you are advised to download the example archives from the Web and unpack them on your computer. This is described further below.

Examples on GitHub

Example NuGet Packages

Some examples (those from the DocExamples and UADocExamples projects, in C# and VB.NET) are also distributed (with source code) in form of NuGet packages, on www.nuget.org . The packages are:

The strings in bold are the package IDs.

Using The Example Packages

The Example Package is not a runnable project on its own. It just contains pieces of source code that you can call or copy into your own project.

In order to run the code from console-based example packages, you need to call it from your project. The examples are organized by the OPC specification, and each example is a static method that you can directly invoke. For easier exploration, there are also methods that provide the user with a menu of methods to choose from.

Alternatively, you can call just a single selected example, e.g. for reading an OPC UA node, call the UADocExamples._EasyUAClient.Read.Main1 method.

Example Archive Files

Examples for .NET can be downloaded directly from the Web, in form of archive (.TGZ or .ZIP) files. This distribution method can be used without running any installer, on all operating systems. The archive files available are:

Note: F# examples are only available for .NET 6+. You can, however, use the same F# source files in .NET Framework, and recreate the example project in .NET Framework, if you need.

The examples are delivered in source form only. If you want to run them, having installed the proper .NET runtime is not enough - you also need the corresponding SDK. For .NET 6 or 8:

Retrieving the Example Archives

In order to use the example archives, first download them to your system, and extract the files from the archive. With a GUI on Windows, you can download the files using a Web browser, and uncompress the .ZIP with built-in Windows Explorer (shell) commands. You may have similar features on your Linux, depending on what distribution you chose. If you don't, following commands can be used in the terminal to download and extract the archives (the URL and file names are just examples - replace them with the right ones if you need a different set of examples):

wget "http://cdn.opclabs.com/files/downloads/OPCLabs-OpcStudio/2024.2/Examples-OpcStudio-CSharp.tgz"
tar -xvzf Examples-OpcStudio-CSharp.tgz

On macOS, and on Linux systems where wget is not available, you can use curl -O instead.

Working with the Examples

You can now open the solutions or projects from your IDE, view and modify their code, or run them.

If you just want to run the examples from the command line without inspecting their source code, first change your current directory to the project type of your choice, e.g.:

cd Console

Then, build and launch the chosen project, e.g.:

dotnet run --project Client-UADocExamples/UADocExamples.csproj

or

dotnet run --project Client-DocExamples/DocExamples.csproj

Categories

Following categories of .NET examples are available:

PowerShell Execution Policy

The default PowerShell settings only allow execution of scripts signed by a trusted authority. Use the following PowerShell command to enable execution of OPC Studio example scripts:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

 

See Also

Fundamentals