Nucleus handler tools โ€บ Docs โ€บ Context โ€บ ReplaceLinesInTextFile

Context.ReplaceLinesInTextFile

Open in the workbench (search, and try it in a handler)

Context.ReplaceLinesInTextFile(
  path: string,
  newLines: string[]
): boolean
Context.ReplaceLinesInTextFile(
  path: string,
  newLines: string[],
  encoder: string
): boolean

Replace an entire line; for string array use the format: "lineNum|newValue", the | is required.

AI-writtenAI-written. Based on: Nucleus GenericContext.cs (opens in a new tab) (ReplaceLinesInTextFile (both overloads)).
  • path: Full path of the text file to edit; it must exist. The file is read, deleted and written back.
  • newLines: Array of "N|new text" strings: N is the line number, counting from 1; out-of-range lines are skipped and text after a second | is dropped.
  • encoder: Encoding name for .NET Encoding.GetEncoding, e.g. "utf-8" or "utf-16", used to read and write; without it .NET's default (UTF-8) is used.

Category: Files

AI-writtenAI-written. Based on: Nucleus GenericContext.cs (opens in a new tab) (ReplaceLinesInTextFile: line.Split('|')); Verified Hub handlers, counted 2026-11-17 (opens in a new tab).

In practice

Each entry is "<line number>|<new line>" (1-based); entries whose number is outside the file (such as -1 from Context.FindLineNumberInTextFile finding nothing) are skipped, and only the text up to a second | is kept. The usual pattern, in Game.Play, builds the list with Context.FindLineNumberInTextFile, often to put the player's nickname or resolution into a game's plain-text config.

Is "In practice" right? ๐Ÿ‘ค People: no votes yet ๐Ÿค– AIs: no votes yet Score 0: peopleโ€™s ๐Ÿ‘ minus ๐Ÿ‘Ž (AIs never count).

By engine: rarer in Unity games.

AI-writtenAI-written. Based on: Nucleus GenericContext.cs (opens in a new tab) (ReplaceLinesInTextFile: line.Split('|')); Verified Hub handlers, counted 2026-11-17 (opens in a new tab).

Example

var cfg =
  Context.GetFolder(Nucleus.Folder.InstancedGameFolder) +
  "\\config.cfg";
Context.ReplaceLinesInTextFile(cfg, [
  Context.FindLineNumberInTextFile(
    cfg,
    "name=",
    Nucleus.SearchType.StartsWith
  ) +
  "|name=" +
  Context.Nickname,
  Context.FindLineNumberInTextFile(
    cfg,
    "width=",
    Nucleus.SearchType.StartsWith
  ) +
  "|width=" +
  Context.Width
]);
Is the doc right? ๐Ÿ‘ค People: no votes yet ๐Ÿค– AIs: no votes yet Score 0: peopleโ€™s ๐Ÿ‘ minus ๐Ÿ‘Ž (AIs never count).

Handler API docs

Stuck, or not sure? Ask other handler authors: Discordr/nucleuscoop