Best way to store data locally in .NET (C#)

后端 未结 19 1246
南方客
南方客 2020-11-28 01:53

I\'m writing an application that takes user data and stores it locally for use later. The application will be started and stopped fairly often, and I\'d like to make it save

相关标签:
19条回答
  • 2020-11-28 02:18

    I'd store the file as JSON. Since you're storing a dictionary which is just a name/value pair list then this is pretty much what json was designed for.
    There a quite a few decent, free .NET json libraries - here's one but you can find a full list on the first link.

    0 讨论(0)
  • 2020-11-28 02:19

    I have done several "stand alone" apps that have a local data store. I think the best thing to use would be SQL Server Compact Edition (formerly known as SQLAnywhere).

    It's lightweight and free. Additionally, you can stick to writing a data access layer that is reusable in other projects plus if the app ever needs to scale to something bigger like full blown SQL server, you only need to change the connection string.

    0 讨论(0)
  • 2020-11-28 02:22

    My first inclination is an access database. The .mdb files are stored locally, and can be encrypted if that is deemed necessary. Though XML or JSON would also work for many scenarios. Flat files I would only use for read only, non-search (forward read only) information. I tend to prefer csv format to set width.

    0 讨论(0)
  • 2020-11-28 02:23

    A lot of the answers in this thread attempt to overengineer the solution. If I'm correct, you just want to store user settings.

    Use an .ini file or App.Config file for this.

    If I'm wrong, and you are storing data that is more than just settings, use a flat text file in csv format. These are fast and easy without the overhead of XML. Folks like to poo poo these since they aren't as elegant, don't scale nicely and don't look as good on a resume, but it might be the best solution for you depending on what you need.

    0 讨论(0)
  • 2020-11-28 02:25

    XML is easy to use, via serialization. Use Isolated storage.

    See also How to decide where to store per-user state? Registry? AppData? Isolated Storage?

    public class UserDB 
    {
        // actual data to be preserved for each user
        public int A; 
        public string Z; 
    
        // metadata        
        public DateTime LastSaved;
        public int eon;
    
        private string dbpath; 
    
        public static UserDB Load(string path)
        {
            UserDB udb;
            try
            {
                System.Xml.Serialization.XmlSerializer s=new System.Xml.Serialization.XmlSerializer(typeof(UserDB));
                using(System.IO.StreamReader reader= System.IO.File.OpenText(path))
                {
                    udb= (UserDB) s.Deserialize(reader);
                }
            }
            catch
            {
                udb= new UserDB();
            }
            udb.dbpath= path; 
    
            return udb;
        }
    
    
        public void Save()
        {
            LastSaved= System.DateTime.Now;
            eon++;
            var s= new System.Xml.Serialization.XmlSerializer(typeof(UserDB));
            var ns= new System.Xml.Serialization.XmlSerializerNamespaces();
            ns.Add( "", "");
            System.IO.StreamWriter writer= System.IO.File.CreateText(dbpath);
            s.Serialize(writer, this, ns);
            writer.Close();
        }
    }
    
    0 讨论(0)
  • 2020-11-28 02:25

    If you go the binary serialization route, Consider the speed at which a particular member of the datum needs to be accessed. If it is only a small collection, loading the whole file will make sense, but if it will be large, you might also consider an index file.

    Tracking Account Properties/fields that are located at a specific address within the file can help you speed up access time, especially if you optimize that index file based on key usage. (possibly even when you write to disk.)

    0 讨论(0)
提交回复
热议问题