S&box Wiki

Revision Difference

FileSystem#563100

<cat>Code.Misc</cat>⤶ <title>FileSystem</title>⤶ ⤶ Standard .NET file access is restricted to prevent rogue access to your files, this means you can not use System.IO.File or variants directly.⤶ ⤶ Instead, s&box provides a [BaseFileSystem](https://asset.party/api/Sandbox.BaseFileSystem) for several virtual filesystems that can only access files within specific game directories.⤶ ⤶ ```csharp⤶ // Example of using a FileSystem to read/write a file⤶ if ( !FileSystem.Data.FileExists( "player.txt" ) )⤶ FileSystem.Data.WriteAllText( "player.txt", "hello world" );⤶ ⤶ var hello = FileSystem.Data.ReadAllText( "player.txt" );⤶ ```⤶ ⤶ ```csharp⤶ // Example of using a FileSystem to save and load player data.⤶ class PlayerData⤶ {⤶ // ...⤶ ⤶ public static void Save( PlayerData data )⤶ {⤶ FileSystem.Data.WriteJson( "player_data.json", data );⤶ }⤶ ⤶ public static PlayerData Load()⤶ {⤶ return FileSystem.Data.ReadJson<PlayerData>( "player_data.json" );⤶ }⤶ }⤶ ⤶ ```⤶ ⤶ <note>⤶ WriteJson / ReadJson will only work with properties of your class unless directed not to - make sure the things you want to save are properties!⤶ ⤶ ```csharp⤶ public class PlayerData⤶ {⤶ public int Level { get; set; } // fine⤶ public int MaxHealth { get; set; } // fine⤶ public string Username; // bad !!⤶ }⤶ ```⤶ ⤶ </note>⤶ ⤶ ### FileSystem.Mounted⤶ ⤶ [FileSystem.Mounted](https://asset.party/api/Sandbox.FileSystem.Mounted) is an aggregate filesystem of all mounted content from the core game, the current gamemode and its dependencies. It's a combination of the following directories:⤶ ⤶ * `sbox\core\`⤶ * `sbox\addons\myaddon\`⤶ * `sbox\addons\myaddon\code\`⤶ * `sbox\addons\base\`⤶ * `sbox\addons\base\code\`⤶ * `sbox\addons\citizen\`⤶ * `sbox\addons\citizen\code\`⤶ * `sbox\addons\rust\`⤶ * `sbox\addons\rust\code\`⤶ ⤶ Any time you try to read a file it will search each of these paths.⤶ ⤶ ### FileSystem.Data⤶ ⤶ [FileSystem.Data](https://asset.party/api/Sandbox.FileSystem.Data) is a place to store user data for your game.⤶ ⤶ `C:\Steam\steamapps\common\sbox\data\org\game\`⤶ ⤶ ### FileSystem.OrganizationData⤶ ⤶ [FileSystem.OrganizationData](https://asset.party/api/Sandbox.FileSystem.OrganizationData) is a place to store user data across several games in your organization.⤶ ⤶ `C:\Steam\steamapps\common\sbox\data\org\`Moved page to [sbox.game](https://sbox.game/dev/doc/file-system/)