    

 ## On this page

  

 

 # Example 26 - C# 

 Last update: 17.07.2025 

# <a class="anchor" id="ex26_s1"></a>C#

using System;

using [ZOSAPI](namespace_z_o_s_a_p_i.xhtml);

using [ZOSAPI.Preferences](namespace_z_o_s_a_p_i_1_1_preferences.xhtml);

 

namespace CSharpStandaloneApplication4

{

 class Program

 {

 static void Main(string[] args)

 {

 // Find the installed version of OpticStudio

 bool isInitialized = ZOSAPI_NetHelper.ZOSAPI_Initializer.Initialize();

 // Note -- uncomment the following line to use a custom initialization path

 //bool isInitialized = ZOSAPI\_NetHelper.ZOSAPI\_Initializer.Initialize(@"C:\\Program Files\\OpticStudio\\");

 if (isInitialized)

 {

 LogInfo("Found OpticStudio at: " + ZOSAPI_NetHelper.ZOSAPI_Initializer.GetZemaxDirectory());

 }

 else

 {

 HandleError("Failed to locate OpticStudio!");

 return;

 }

 

 BeginStandaloneApplication();

 }

 

 static void BeginStandaloneApplication()

 {

 // Create the initial connection class

 [ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml) TheConnection = new [ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)();

 

 // Define a Preferences File.

 // Preferences file is defined on the IZOSAPIConnection interface (prior to connecting to the API)

 // If no PreferencesFile is defined it will use the default OpticStudio.CFG file however changes will not persist between sessions. 

 // If a PreferencesFile is defined, then any changes will save automatically. 

 Console.WriteLine("===PreferencesFile===");

 string cfgFile = @"C:\\Users\\Documents\\Zemax\\Configs\\OpticStudio.CFG";

 if (System.IO.File.Exists(cfgFile))

 {

 TheConnection.PreferencesFile = cfgFile;

 Console.WriteLine("PreferencesFile: " + TheConnection.[PreferencesFile](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#a18dc1b1ef3fac28798acc2ecdedd3e08));

 }

 else

 {

 Console.WriteLine("Default OpticStudio.CFG prefernces used");

 }

 

 // Attempt to create a Standalone connection

 [IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml) TheApplication = TheConnection.[CreateNewApplication](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#aadc54af7d7415a765592acf23d3a08ef)();

 if (TheApplication == null)

 {

 HandleError("An unknown connection error occurred!");

 return;

 }

 

 // Check the connection status

 if (!TheApplication.[IsValidLicenseForAPI](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a5b8d6c216c668d13d6dcb870698c247f))

 {

 HandleError("Failed to connect to OpticStudio: " + TheApplication.[LicenseStatus](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a95179eb8038b629caa12397723aacb3a));

 return;

 }

 if (TheApplication.[Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19) != [ZOSAPI\_Mode](namespace_z_o_s_a_p_i.xhtml#a254f5f6cb4ff37c2245b456b9bb79d2a).Server)

 {

 HandleError("User plugin was started in the wrong mode: expected Server, found " + TheApplication.[Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19).ToString());

 return;

 }

 

 [IOpticalSystem](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml) TheSystem = TheApplication.[PrimarySystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#aca8045fe84351622e0ee58814a2a3364);

 

 // Add your custom code here...

 

 // Define variables for OpticStudio Preferences

 [IPreferences](interface_z_o_s_a_p_i_1_1_i_preferences.xhtml) Preference = TheApplication.[Preferences](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a2b0d83df3552a7647a57a22a9496f43d);

 [IPreferencesGeneral](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml) PrefG = Preference.[General](interface_z_o_s_a_p_i_1_1_i_preferences.xhtml#a962ef89db42273aca29102b6fb812d4a);

 

 // Read and print the initial settings

 Console.WriteLine("\\n===Check Settings===");

 Console.WriteLine("DateTimeFormat: " + PrefG.[DateTimeFormat](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a08c9851a5c9e95249e247964f4d8246c));

 Console.WriteLine("Language: " + PrefG.[Language](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#acdb183b72e7c6d518ab2498266d96120));

 Console.WriteLine("ZMXFileEncoding: " + PrefG.[ZMXFileEncoding](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a00b869844d613fba6402c2e08433eb7a));

 Console.WriteLine("TXTFileEncoding: " + PrefG.[TXTFileEncoding](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a32c4bdb3c87442c8e25f1b3fff721c68));

 Console.WriteLine("UseSessionFiles: " + PrefG.[UseSessionFiles](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a05297c825e3d5db4b61b39f312fd0e07));

 Console.WriteLine("IncludeCalculatedDataInsession: " + PrefG.[IncludeCalculatedDataInSession](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a5dedeeb661115644cc06de1688cf5c95));

 Console.WriteLine("UpdateMostRecentlyUsedList: " + PrefG.[UpdateMostRecentlyUsedList](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a2efa6ee6fe4db0f5cc839ab473462272));

 Console.WriteLine("UserPreferences: " + PrefG.[UserPreferences](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#ac46784a53f375871f7050942ea3ef76b));

 

 // Reset the settings to default

 Preference.[ResetToDefaults](interface_z_o_s_a_p_i_1_1_i_preferences.xhtml#a46785d61c4607d6b571022ac4a47f208)();

 

 // Set the settings 

 PrefG.DateTimeFormat = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Preferences](namespace_z_o_s_a_p_i_1_1_preferences.xhtml).DateTimeType.DateTime;

 PrefG.Language = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Preferences](namespace_z_o_s_a_p_i_1_1_preferences.xhtml).LanguageType.English;

 PrefG.ZMXFileEncoding = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Preferences](namespace_z_o_s_a_p_i_1_1_preferences.xhtml).EncodingType.ANSI;

 PrefG.TXTFileEncoding = [ZOSAPI](namespace_z_o_s_a_p_i.xhtml).[Preferences](namespace_z_o_s_a_p_i_1_1_preferences.xhtml).EncodingType.ANSI;

 PrefG.UseSessionFiles = false;

 PrefG.IncludeCalculatedDataInSession = false;

 PrefG.UpdateMostRecentlyUsedList = false;

 PrefG.UserPreferences = "Never gonna run around and desert you";

 

 Console.WriteLine("\\n===Final Settings===");

 Console.WriteLine("DateTimeFormat: " + PrefG.[DateTimeFormat](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a08c9851a5c9e95249e247964f4d8246c));

 Console.WriteLine("Language: " + PrefG.[Language](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#acdb183b72e7c6d518ab2498266d96120));

 Console.WriteLine("ZMXFileEncoding: " + PrefG.[ZMXFileEncoding](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a00b869844d613fba6402c2e08433eb7a));

 Console.WriteLine("TXTFileEncoding: " + PrefG.[TXTFileEncoding](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a32c4bdb3c87442c8e25f1b3fff721c68));

 Console.WriteLine("UseSessionFiles: " + PrefG.[UseSessionFiles](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a05297c825e3d5db4b61b39f312fd0e07));

 Console.WriteLine("IncludeCalculatedDataInsession: " + PrefG.[IncludeCalculatedDataInSession](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a5dedeeb661115644cc06de1688cf5c95));

 Console.WriteLine("UpdateMostRecentlyUsedList: " + PrefG.[UpdateMostRecentlyUsedList](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a2efa6ee6fe4db0f5cc839ab473462272));

 Console.WriteLine("UserPreferences: " + PrefG.[UserPreferences](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#ac46784a53f375871f7050942ea3ef76b));

 

 Console.WriteLine("\\nPress any key to continue...");

 Console.ReadKey();

 

 

 

 

 // Clean up

 FinishStandaloneApplication(TheApplication);

 }

 

 static void FinishStandaloneApplication([IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml) TheApplication)

 {

 // Note - TheApplication will close automatically when this application exits, so this isn't strictly necessary in most cases

 if (TheApplication != null)

 {

 TheApplication.[CloseApplication](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ab662a3cc1b97d8632a324b1137fa943b)();

 }

 }

 

 static void LogInfo(string message)

 {

 // TODO - add custom logging

 Console.WriteLine(message);

 }

 

 static void HandleError(string errorMessage)

 {

 // TODO - add custom error handling

 throw new Exception(errorMessage);

 }

 

 }

}

[ZOSAPI.ZOSAPI\_Connection](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml)

**Definition:** ZemaxService.cs:198



[ZOSAPI.ZOSAPI\_Connection.PreferencesFile](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#a18dc1b1ef3fac28798acc2ecdedd3e08)

string PreferencesFile

For standalone ZOS-API applications, this controls the file that will be used to specify all OpticStu...

**Definition:** ZemaxService.cs:1340



[ZOSAPI.ZOSAPI\_Connection.CreateNewApplication](class_z_o_s_a_p_i_1_1_z_o_s_a_p_i___connection.xhtml#aadc54af7d7415a765592acf23d3a08ef)

IZOSAPI_Application CreateNewApplication()

Attempts to launch a new instance of Optic Studio in 'headless' mode. Note that although the Optic St...

**Definition:** ZemaxService.cs:864



[ZOSAPI.IOpticalSystem](interface_z_o_s_a_p_i_1_1_i_optical_system.xhtml)

Represent a complete optical system. A IOpticalSystem corresponds to a single .ZMX file....

**Definition:** Interfaces.cs:687



[ZOSAPI.IPreferences](interface_z_o_s_a_p_i_1_1_i_preferences.xhtml)

**Definition:** InterfacesPreferences.cs:16



[ZOSAPI.IPreferences.ResetToDefaults](interface_z_o_s_a_p_i_1_1_i_preferences.xhtml#a46785d61c4607d6b571022ac4a47f208)

void ResetToDefaults()



[ZOSAPI.IPreferences.General](interface_z_o_s_a_p_i_1_1_i_preferences.xhtml#a962ef89db42273aca29102b6fb812d4a)

IPreferencesGeneral General

**Definition:** InterfacesPreferences.cs:18



[ZOSAPI.IZOSAPI\_Application](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml)

This interface contains all information about the current ZOS-API connection, as well as methods for ...

**Definition:** Interfaces.cs:261



[ZOSAPI.IZOSAPI\_Application.Preferences](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a2b0d83df3552a7647a57a22a9496f43d)

IPreferences Preferences

**Definition:** Interfaces.cs:642



[ZOSAPI.IZOSAPI\_Application.IsValidLicenseForAPI](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a5b8d6c216c668d13d6dcb870698c247f)

bool IsValidLicenseForAPI

Gets a value indicating whether this the API is currently useable.

**Definition:** Interfaces.cs:287



[ZOSAPI.IZOSAPI\_Application.Mode](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a8d2a89bafe9778c14f592276e5a5eb19)

ZOSAPI_Mode Mode

Gets the current connetion mode. Use this to check if Optic Studio is expecting a user operand / anal...

**Definition:** Interfaces.cs:306



[ZOSAPI.IZOSAPI\_Application.LicenseStatus](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#a95179eb8038b629caa12397723aacb3a)

LicenseStatusType LicenseStatus

Gets the license status. Note that this displays the license edition if successful,...

**Definition:** Interfaces.cs:280



[ZOSAPI.IZOSAPI\_Application.CloseApplication](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#ab662a3cc1b97d8632a324b1137fa943b)

void CloseApplication()

Shut down the Optic Studio process.



[ZOSAPI.IZOSAPI\_Application.PrimarySystem](interface_z_o_s_a_p_i_1_1_i_z_o_s_a_p_i___application.xhtml#aca8045fe84351622e0ee58814a2a3364)

IOpticalSystem PrimarySystem

Gets the primary system. When Mode is ZOSAPI_Mode.Server, this will initially be an empty sequential ...

**Definition:** Interfaces.cs:328



[ZOSAPI.Preferences.IPreferencesGeneral](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml)

Allows configuration of settings from the OpticStudio Preferences -&gt; General tab in OpticStudio....

**Definition:** InterfacesPreferences.cs:89



[ZOSAPI.Preferences.IPreferencesGeneral.ZMXFileEncoding](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a00b869844d613fba6402c2e08433eb7a)

EncodingType ZMXFileEncoding

**Definition:** InterfacesPreferences.cs:92



[ZOSAPI.Preferences.IPreferencesGeneral.UseSessionFiles](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a05297c825e3d5db4b61b39f312fd0e07)

bool UseSessionFiles

**Definition:** InterfacesPreferences.cs:94



[ZOSAPI.Preferences.IPreferencesGeneral.DateTimeFormat](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a08c9851a5c9e95249e247964f4d8246c)

DateTimeType DateTimeFormat

**Definition:** InterfacesPreferences.cs:90



[ZOSAPI.Preferences.IPreferencesGeneral.UpdateMostRecentlyUsedList](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a2efa6ee6fe4db0f5cc839ab473462272)

bool UpdateMostRecentlyUsedList

This setting can be used to turn updating of the most recently used lens files in File &gt; Open on or o...

**Definition:** InterfacesPreferences.cs:100



[ZOSAPI.Preferences.IPreferencesGeneral.TXTFileEncoding](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a32c4bdb3c87442c8e25f1b3fff721c68)

EncodingType TXTFileEncoding

**Definition:** InterfacesPreferences.cs:93



[ZOSAPI.Preferences.IPreferencesGeneral.IncludeCalculatedDataInSession](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#a5dedeeb661115644cc06de1688cf5c95)

bool IncludeCalculatedDataInSession

**Definition:** InterfacesPreferences.cs:95



[ZOSAPI.Preferences.IPreferencesGeneral.UserPreferences](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#ac46784a53f375871f7050942ea3ef76b)

string UserPreferences

This is a single string in which you can store anything you want. It is saved in your Preferences con...

**Definition:** InterfacesPreferences.cs:104



[ZOSAPI.Preferences.IPreferencesGeneral.Language](interface_z_o_s_a_p_i_1_1_preferences_1_1_i_preferences_general.xhtml#acdb183b72e7c6d518ab2498266d96120)

LanguageType Language

**Definition:** InterfacesPreferences.cs:91



[ZOSAPI.Preferences](namespace_z_o_s_a_p_i_1_1_preferences.xhtml)

**Definition:** InterfacesPreferences.cs:30



[ZOSAPI](namespace_z_o_s_a_p_i.xhtml)

The ZOSAPI namespace contains classes for initially connecting to zemax. See also ZOSAPI_Connection,...

**Definition:** IAS_FieldCurvatureAndDistortion.cs:5



[ZOSAPI.ZOSAPI\_Mode](namespace_z_o_s_a_p_i.xhtml#a254f5f6cb4ff37c2245b456b9bb79d2a)

ZOSAPI_Mode

**Definition:** Interfaces.cs:77