BlackBytesBox.Unified.Core
  • API
Show / Hide Table of Contents
  • BlackBytesBox.Unified.Core.Extensions.StringExtensions
    • StringExtensions
    • ValidationsStringExtensions
  • BlackBytesBox.Unified.Core.Utilities.MainContext
    • MainContext

Class MainContext

Provides utility methods for managing the application's execution context, primarily for use within the Program.Main entry point.

Inheritance
object
MainContext
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: BlackBytesBox.Unified.Core.Utilities.MainContext
Assembly: BlackBytesBox.Unified.Core.dll
Syntax
public static class MainContext

Methods

| Edit this page View Source

EnsureSingleInstance(Action, bool)

Ensures that only a single instance of the application is running. If another instance is detected, the specified duplicate instance action is executed, and optionally the application is terminated.

Declaration
public static void EnsureSingleInstance(Action duplicateInstanceAction, bool exitOnDuplicate = true)
Parameters
Type Name Description
Action duplicateInstanceAction

An Action to execute if a duplicate instance is detected. This action can be used to notify the user or log the occurrence of a duplicate instance.

bool exitOnDuplicate

A boolean value indicating whether to terminate the application when a duplicate instance is detected. Defaults to true.

Remarks

The method attempts to create a named Mutex using the application's entry assembly name. If the mutex already exists, it indicates that another instance of the application is running.

Examples
// Ensure single instance; if a duplicate is detected, notify the user and exit.
MainContext.EnsureSingleInstance(() => Console.WriteLine("Another instance is already running."));
| Edit this page View Source

TryGetOrCreateAppNameDirectory(string[]?, bool, string[]?, bool)

Retrieves or creates a directory based on the application's name, which is derived from the primary file location. Optionally, a unique identifier is appended to the directory name when the useStartupAppLocation flag is set to true.

Declaration
public static DirectoryInfo? TryGetOrCreateAppNameDirectory(string[]? candidateBaseDirectories = null, bool useStartupAppLocation = false, string[]? subdirectoriesToEnsure = null, bool throwIfFails = false)
Parameters
Type Name Description
string[] candidateBaseDirectories

An optional array of candidate base directories where the application directory should be searched for or created. If not provided, a default set of system directories will be used.

bool useStartupAppLocation

When set to true, appends a unique identifier (derived from the primary file location) to the application directory name.

string[] subdirectoriesToEnsure

An optional array of subdirectory names that should be ensured (created if missing) within the application directory.

bool throwIfFails

Indicates whether an exception should be thrown if the operation to retrieve or create the directory fails.

Returns
Type Description
DirectoryInfo

A DirectoryInfo object representing the application directory if successfully retrieved or created; otherwise, null.

Examples
// Retrieve or create the application directory using default settings.
var configDirectory = MainContext.TryGetOrCreateAppNameDirectory();
| Edit this page View Source

TryGetPrimaryFileLocation()

Attempts to identify and return the primary file location of the currently executing application.

Declaration
public static string? TryGetPrimaryFileLocation()
Returns
Type Description
string

The file path of the main module if its name matches the domain, or the location of the entry or calling assembly. Returns null if none of these locations can be determined.

Remarks

This method first tries to match the main executable's name with the current domain name. If no match is found, it attempts to retrieve the location from the entry assembly or, if that is not available, from the calling assembly. This method is useful for applications that need to determine the path of the currently running executable or assembly.

  • Edit this page
  • View Source
In this article
Back to top