Work with files, resources and preferences
Choose paths by ownership, preserve cancellation and keep private settings separate from distributable resources.
Use the right location#
| Location | Purpose | Rule |
|---|---|---|
| App.ResourcePath | Assets packaged with the application | Read them; do not write back into the bundle. |
| AppPaths.DataFile(relativePath) | Per-application writable files | Use relative application data names, not traversal outside the application area. |
| A native picker result | A file or folder chosen by the user | Cancel returns no selection; check before reading or writing. |
| Settings | Small per-user scalar preferences | Not a secret store and not a replacement for a database. |
Read and write deliberately#
File.ReadText and File.WriteText use strict UTF-8. A failed decode is a real error, not a reason to silently replace characters. The output directory must exist. File.WriteText replaces the target’s contents, so use it only for an intended destination. Use a disposable practice file in the service workbench.
Keep binary data binary#
ReadBytes returns a byte array. WriteBytes expects an allocated one-dimensional Byte array. Do not pass a text conversion of an image or a media file. Large downloads belong in the streaming download workflow rather than reading the entire response into a text variable.
Treat Cancel as a valid result#
Dim selected As String
selected = Dialogs.OpenFile("Choose a text file", "Text|*.txt")
If Len(selected) = 0 Then Exit Sub
txtNotes.Text = File.ReadText(selected)A file can change or become inaccessible after selection. Handle the read/write failure even when the picker succeeded. The CommonDialog lesson covers the component-based dialog API as well.
Give preferences a default#
Dim lastName As String
lastName = CStr(Settings.Get("last-name", ""))
Settings.Set "last-name", "Ada"Settings persists supported scalar values. Use stable keys, provide a default when reading and avoid storing passwords, access tokens or entire object graphs. Logging should describe the action and outcome without copying those private values.
Keep platform integration bounded#
Path performs path manipulation without shell execution. Clipboard handles text exchange with the system clipboard. Platform exposes platform facts and supported URL opening. Environment intentionally exposes a controlled development/CI allowlist, not the entire process environment or secret keys.
20f4bfed6b · Build manifest