Version: Unity 6.6 Alpha (6000.6)
LanguageEnglish
  • C#

FileUtil.OpenRead

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static Stream OpenRead(string path);

Parameters

Parameter Description
path Path to the file. Accepts logical and physical paths.

Returns

Stream A read-only System.IO.Stream over the file content. Dispose the stream when finished.

Description

Opens a file for reading, resolving logical paths automatically.

Returns a readable System.IO.Stream for the file at path. Unlike File.Open, this method accepts logical paths directly without first converting them with FileUtil.PathToAbsolutePath.

Throws System.ArgumentException when path is null or empty. Throws System.IO.IOException when the file cannot be opened.

Additional resources: FileUtil.ReadAllBytes, FileUtil.ReadAllText, FileUtil.ReadAllLines, FileUtil.PathToAbsolutePath

using System.IO;
using UnityEditor;
using UnityEngine;

public class OpenReadExample { [MenuItem("Example/Read File As Stream")] static void ReadAsStream() { using (Stream stream = FileUtil.OpenRead("Packages/com.example.package/Resources/config.bytes")) using (var reader = new BinaryReader(stream)) { int version = reader.ReadInt32(); Debug.Log("Config version: " + version); } } }