Scripting in Application (C#)
In this article: A scripting language based on C# that takes advantage of the full power of .NET. Please refer to here for more information about the language.Language Version
ULogViewer
- C# 10: ULogViewer 2.0
- C# 11: ULogViewer 3.0
- C# 12: ULogViewer 4.0
- C# 14: ULogViewer 2026.0
PixelViewer
- C# 13, C# 14: PixelViewer 2026.1
Namespaces
The following namespaces are imported by default:- System
- System.Collections.Generic
- System.Linq
- Microsoft.Extensions.Logging
-
CarinaStudio
To access types and functions provided by CarinaStudio.AppBase.Core.
-
CarinaStudio.Collections
To access types and functions provided by CarinaStudio.AppBase.Core.
Global Variables
There are 3 global variables in script runtime environment:App
Allows scripts to access application-level functions. The following are members of App:- ExecuteCommand(string command, CancellationToken cancellationToken): int
-
ExecuteCommand(string command, Action<Process, CancellationToken> action, CancellationToken cancellationToken): int
Execute external command and get exit code of command.action is a call-back function to let you send data to standard input and read data from standard output/error from process of command, for example:
App.ExecuteCommand("my_command", (process, cancellationToken) => { var reader = process.StandardOutput; var line = reader.ReadLine(); while (line is not null) { // Process the read line... line = reader.ReadLine(); } }); -
FindCommandPath(string command, CancellationToken cancellationToken): string?
Find absolute path to the given command. Will returns null if command cannot be found.
-
GetFormattedString(string key, params object?[] args): string?
Get format defined in application resources and generate formatted string with given arguments.
- GetString(string key): string?
-
GetString(string key, string? defaultString): string?
Get string resource with given key.
- GetStringNonNull(string key): string
-
GetStringNonNull(string key, string defaultString): string
Get string resource with given key which makes sure that the string won't be null.
-
IsDebugMode: bool
Check whether application is running in debug mode or not.
-
IsMainThread: bool
Check whether current thread is the main thread of application or not.
-
MainThreadSynchronizationContext: SynchronizationContext
Get System.Threading.SynchronizationContext of main thread of application.
CancellationToken
System.Threading.CancellationToken for checking whether cancellation of script running has been requested or not.Context
Allows accessing functions according to the purpose of the script. The actual instance of context is also determined by the purpose of the script. The following are basic members of Context:
The members which show a dialog (ShowMessageDialog and the other Show* members) are available only when the context of the script is user-interactive.
They are not available to image rendering scripts and demosaicing scripts of PixelViewer, which run on a background thread every time when the image needs to be rendered.
-
Data: IDictionary<string, object>
A dictionary which stores custom data. The data may be transferred across scripts if scripts belong to same set of same purpose.
- GetString(string key): string?
-
GetString(string key, string? defaultString): string?
Get the string with given key defined in context by calling PrepareStrings(). Will fallback to string defined in application if it cannot be found in context, or fallback to defaultString if string cannot be found in application either.
-
Logger: ILogger
Allow writing log to log buffer and file of the application.
- PrepareStrings(Action<IDictionary<string, string>> preparation)
-
PrepareStrings(string? cultureName, Action<IDictionary<string, string>> preparation)
Setup string table for a specific culture. The format of cultureName is languagecode2-country/regioncode2. For example, en-US. You can also pass null to cultureName to define default string table.preparation is a call-back function to setup string table. For example:
Context.PrepareStrings(null, table => { table["Message"] = "Hello"; }); Context.PrepareStrings("zh-TW", table => { table["Message"] = "哈囉"; });String tables are dedicated for each context. Therefore, string tables setup through PrepareStrings() can only be accessed by its context. - ShowMultipleItemsSelectionDialog(string? message, IList items): IList<int>ULogViewer 4.0+
-
ShowMultipleItemsSelectionDialog(string? message, IList items, int defaultItemIndex): IList<int>ULogViewer 4.0+
Show dialog to let user select one or more items. The returned value is the list of indices of selected items.
- ShowSingleItemSelectionDialog(string? message, IList items): intULogViewer 4.0+
-
ShowSingleItemSelectionDialog(string? message, IList items, int defaultItemIndex): intULogViewer 4.0+
Show dialog to let user select one item. The returned value is the index of selected item, or -1 if nothing is selected.
- ShowMessageDialog(string? message): MessageDialogResult
- ShowMessageDialog(string? message, MessageDialogIcon icon): MessageDialogResult
-
ShowMessageDialog(string? message, MessageDialogIcon icon, MessageDialogButtons buttons): MessageDialogResult
Shows message dialog and wait for result selected by user.Values defined in MessageDialogIcon:
- Error
- Information (default)
- Question
- Success
- Warning
- OK (default)
- OKCancel
- YesNo
- YesNoCancel
- Cancel
- No
- OK
- Yes
- ShowTextInputDialog(string? message): string?
-
ShowTextInputDialog(string? message, string? initText): string?
Shows text input dialog and wait for text input by user.