Show / Hide Table of Contents

Class ModIO

Provides methods for IO since large parts of the System.IO namespace are blocked for security reasons.

Inheritance
Object
ModIO
Namespace: Modding
Assembly: Assembly-CSharp.dll
Syntax
public static class ModIO
Remarks

You can only access files located in your mod's directory or its data directory with this class. All paths are relative to these directories. Which directory is used as base is determined by the data (boolean) parameter that all methods that take a path have. It defaults to false, i.e. using the mod root directory.

Use the data directory for files that are meant to be stored persistently, as file in the mod directory could be overwritten by Steam at any time if the mod is installed from the workshop.

Apart from that, methods are mostly analogous to the methods of the same name in the System.IO.File, System.IO.Directory, and System.Net.WebClient classes. The methods corresponding to WebClient methods take event handlers as (optional) arguments, instead of exposing the WebClient object itself. See documentation of those classes for explanations of most parameters.

Methods

AppendText(String, Boolean)

Declaration
public static TextWriter AppendText(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data
Returns
Type Description
TextWriter

CreateDirectory(String, Boolean)

Declaration
public static void CreateDirectory(string path, bool data = false)
Parameters
Type Name Description
String path
Boolean data

CreateText(String, Boolean)

Declaration
public static TextWriter CreateText(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data
Returns
Type Description
TextWriter

DeleteDirectory(String, Boolean, Boolean)

Declaration
public static void DeleteDirectory(string path, bool recursive, bool data = false)
Parameters
Type Name Description
String path
Boolean recursive
Boolean data

DeleteFile(String, Boolean)

Declaration
public static void DeleteFile(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data

DeserializeXml<T>(String, Boolean, Boolean)

Deserializes an XML file using the Modding.Serialization system for optional validation.

Declaration
public static T DeserializeXml<T>(string filePath, bool data = false, bool validate = true)
    where T : Element
Parameters
Type Name Description
String filePath

Path to the XML file.

Boolean data

Whether the path is relative to the data directory or the mod directory.

Boolean validate

Whether the deserialized object should be validated.

Returns
Type Description
T

The deserialized object or null if an error was encountered.

Type Parameters
Name Description
T

Type of object to be deserialized.

Remarks

In case of errors, they are logged to the in-game console and null is returned.

DownloadData(String)

Declaration
public static byte[] DownloadData(string url)
Parameters
Type Name Description
String url
Returns
Type Description
Byte[]

DownloadDataAsync(Uri, DownloadDataCompletedEventHandler, DownloadProgressChangedEventHandler)

Declaration
public static void DownloadDataAsync(Uri uri, DownloadDataCompletedEventHandler downloadComplete, DownloadProgressChangedEventHandler progressChanged = null)
Parameters
Type Name Description
Uri uri
DownloadDataCompletedEventHandler downloadComplete
DownloadProgressChangedEventHandler progressChanged

DownloadFile(String, String, AsyncCompletedEventHandler, DownloadProgressChangedEventHandler, Boolean)

Declaration
public static void DownloadFile(string url, string filePath, AsyncCompletedEventHandler downloadCompleted = null, DownloadProgressChangedEventHandler progressChanged = null, bool data = false)
Parameters
Type Name Description
String url
String filePath
AsyncCompletedEventHandler downloadCompleted
DownloadProgressChangedEventHandler progressChanged
Boolean data

DownloadFileAsync(Uri, String, AsyncCompletedEventHandler, DownloadProgressChangedEventHandler, Boolean)

Declaration
public static void DownloadFileAsync(Uri uri, string filePath, AsyncCompletedEventHandler downloadComplete = null, DownloadProgressChangedEventHandler progressChanged = null, bool data = false)
Parameters
Type Name Description
Uri uri
String filePath
AsyncCompletedEventHandler downloadComplete
DownloadProgressChangedEventHandler progressChanged
Boolean data

DownloadString(String)

Declaration
public static string DownloadString(string url)
Parameters
Type Name Description
String url
Returns
Type Description
String

DownloadStringAsync(Uri, DownloadStringCompletedEventHandler, DownloadProgressChangedEventHandler)

Declaration
public static void DownloadStringAsync(Uri uri, DownloadStringCompletedEventHandler downloadComplete, DownloadProgressChangedEventHandler progressChanged = null)
Parameters
Type Name Description
Uri uri
DownloadStringCompletedEventHandler downloadComplete
DownloadProgressChangedEventHandler progressChanged

ExistsDirectory(String, Boolean)

Declaration
public static bool ExistsDirectory(string path, bool data = false)
Parameters
Type Name Description
String path
Boolean data
Returns
Type Description
Boolean

ExistsFile(String, Boolean)

Declaration
public static bool ExistsFile(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data
Returns
Type Description
Boolean

GetDirectories(String, Boolean)

Declaration
public static string[] GetDirectories(string path, bool data = false)
Parameters
Type Name Description
String path
Boolean data
Returns
Type Description
String[]

GetFiles(String, Boolean)

Declaration
public static string[] GetFiles(string dirPath, bool data = false)
Parameters
Type Name Description
String dirPath
Boolean data
Returns
Type Description
String[]

GetFiles(String, String, Boolean)

Declaration
public static string[] GetFiles(string dirPath, string searchPattern, bool data = false)
Parameters
Type Name Description
String dirPath
String searchPattern
Boolean data
Returns
Type Description
String[]

Open(String, FileMode, Boolean, FileAccess, FileShare)

Declaration
public static Stream Open(string filePath, FileMode mode, bool data = false, FileAccess access = FileAccess.Read | FileAccess.Write | FileAccess.ReadWrite, FileShare share = FileShare.None)
Parameters
Type Name Description
String filePath
FileMode mode
Boolean data
FileAccess access
FileShare share
Returns
Type Description
Stream

OpenFolderInFileBrowser(String, Boolean)

Declaration
public static void OpenFolderInFileBrowser(string path, bool data = false)
Parameters
Type Name Description
String path
Boolean data

OpenRead(String, Boolean)

Declaration
public static Stream OpenRead(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data
Returns
Type Description
Stream

OpenText(String, Boolean)

Declaration
public static TextReader OpenText(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data
Returns
Type Description
TextReader

ReadAllBytes(String, Boolean)

Declaration
public static byte[] ReadAllBytes(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data
Returns
Type Description
Byte[]

ReadAllLines(String, Boolean)

Declaration
public static string[] ReadAllLines(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data
Returns
Type Description
String[]

ReadAllLines(String, Encoding, Boolean)

Declaration
public static string[] ReadAllLines(string filePath, Encoding encoding, bool data = false)
Parameters
Type Name Description
String filePath
Encoding encoding
Boolean data
Returns
Type Description
String[]

ReadAllText(String, Boolean)

Declaration
public static string ReadAllText(string filePath, bool data = false)
Parameters
Type Name Description
String filePath
Boolean data
Returns
Type Description
String

ReadAllText(String, Encoding, Boolean)

Declaration
public static string ReadAllText(string filePath, Encoding encoding, bool data = false)
Parameters
Type Name Description
String filePath
Encoding encoding
Boolean data
Returns
Type Description
String

SerializeXml<T>(T, String, Boolean)

Serializes an Element-derived object to an XML file. If the file path already exists, the file is overwritten.

The serialized XML output will contain lineNumber and linePosition attributes that cannot be avoided due to limitations of the serialization system. These attributes will not interfere with deserialization.

Note: The mod loader itself makes heave use of the Deserialization system but doesn't directly use XML serialization. Because of this there are multiple tricks for deserialization that make it more reliable while serialization hasn't received the same attention. Use at your own risk.

Declaration
public static void SerializeXml<T>(T obj, string filePath, bool data = false)
    where T : Element
Parameters
Type Name Description
T obj
String filePath
Boolean data
Type Parameters
Name Description
T

WriteAllBytes(String, Byte[], Boolean)

Declaration
public static void WriteAllBytes(string filePath, byte[] bytes, bool data = false)
Parameters
Type Name Description
String filePath
Byte[] bytes
Boolean data

WriteAllLines(String, String[], Boolean)

Declaration
public static void WriteAllLines(string filePath, string[] lines, bool data = false)
Parameters
Type Name Description
String filePath
String[] lines
Boolean data

WriteAllText(String, String, Boolean)

Declaration
public static void WriteAllText(string filePath, string text, bool data = false)
Parameters
Type Name Description
String filePath
String text
Boolean data
Back to top Generated by DocFX