How to store data locally in .NET C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
.NET C# offers several options for local data storage, from simple file I/O to embedded databases. The right choice depends on data complexity, size, and whether you need querying capabilities. Simple key-value settings use Properties.Settings or JSON files. Structured data uses SQLite or LiteDB. Binary serialization works for object graphs. This guide covers each approach with practical examples.
File-Based Storage (Text/JSON)
The simplest approach for small amounts of data:
For plain text or CSV:
Application Settings (Properties.Settings)
Built-in settings for WinForms and WPF applications:
Settings are stored in the user's AppData folder as XML. User-scoped settings persist between sessions; application-scoped settings are read-only at runtime.
SQLite (Structured Data)
For relational data with SQL querying:
SQLite with Entity Framework Core
For an ORM approach:
LiteDB (NoSQL Document Store)
A lightweight embedded NoSQL database:
LiteDB stores data as BSON documents in a single file, requires no server, and supports LINQ queries.
Isolated Storage (UWP/Sandbox)
For sandboxed apps like UWP:
Common Pitfalls
- Hardcoding file paths: Using absolute paths like
C:\data\app.dbbreaks on other machines. UseEnvironment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)orAppContext.BaseDirectoryfor portable paths. - Not disposing database connections: SQLite connections and
LiteDatabaseinstances must be disposed. Useusingstatements orusing vardeclarations to prevent file locks and data corruption. - Concurrent file access without locking: Multiple threads writing to the same JSON or text file can cause data corruption. Use
lock,SemaphoreSlim, or a database (SQLite has built-in concurrency handling) for multi-threaded scenarios. - Storing sensitive data in plain text: Passwords, API keys, and tokens should not be stored in JSON or text files. Use
System.Security.Cryptography.ProtectedData(Windows DPAPI) or the platform's secure storage API. - Using
BinaryFormatterfor serialization:BinaryFormatteris deprecated in .NET 8+ due to security vulnerabilities. UseSystem.Text.Json,MessagePack, orprotobuf-netfor binary serialization instead.
Summary
- Use JSON files (
System.Text.Json+File.WriteAllText) for simple structured data - Use
Properties.Settingsfor WinForms/WPF application preferences - Use SQLite (with or without EF Core) for relational data with SQL querying
- Use LiteDB for document-oriented NoSQL storage in a single file
- Store files in
LocalApplicationDatafor portable paths across machines - Never store sensitive data in plain text — use platform-specific secure storage
Related reading
- How to store images using Entity Framework Code First CTP 5?
- How to take all but the last element in a sequence using LINQ?
- How to tell Json.Net globally to apply the StringEnumConverter to all enums
- How to terminate a thread in C?
- How to touch a file in C?
- How to truncate milliseconds off of a .NET DateTime
- How to turn off or handle camelCasing in JSON response ASP.NET Core?
- How to turn off the logging done by the ASP.NET core framework

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.