简体   繁体   中英

How to write/read data from object class to txt c#

I have a class which I need to save to a file. The class looks like this

class GLogInfo
{
    public int tmno;
    public int smno, seno;
    public int vmode;
    public int yr, mon, day, hr, min, sec;

    public string photo { get { return (tmno == -1) ? "No Photo" : Convert.ToString(tmno); } }
    public int enroll { get { return seno; } }
    public int machine { get { return smno; } }

    public string verify_mode
    {
        get
        {
            string attend_status = "";
            switch ((vmode >> 8) & 0xFF)
            {
                case 0: attend_status = "_DutyOn"; break;
                case 1: attend_status = "_DutyOff"; break;

            }

            string antipass = "";
            switch ((vmode >> 16) & 0xFFFF)
            {
                case 1: antipass = "(AP_In)"; break;
                case 2: antipass = "(AP_Out)"; break;
            }

            int vm = vmode & 0xFF;
            string str = "--";
            switch (vm)
            {
                case 1: str = "Fp"; break;

                case 7: str = "FP+Card+Pwd"; break;

            }

            if ((1 <= vm && vm <= 7) ||
                (30 <= vm && vm <= 34) ||
                (51 <= vm && vm <= 53) ||
                (101 <= vm && vm <= 103) ||
                (151 <= vm && vm <= 153))
            {
                str = str + attend_status;
            }

            str += antipass;

            return str;
        }
    }

    public string logtime { get { return string.Format("{0:D4}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", yr, mon, day, hr, min, sec); } }
}

populating the class with data from a device(access control device) using a dll code

           while (true)
           {

                GLogInfo gi = new GLogInfo();
                vRet = sbxpc.SBXPCDLL.GetGeneralLogData(Program.gMachineNumber,
                                                out gi.tmno,
                                                out gi.seno,
                                                out gi.smno,
                                                out gi.vmode,
                                                out gi.yr,
                                                out gi.mon,
                                                out gi.day,
                                                out gi.hr,
                                                out gi.min,
                                                out gi.sec);
                if (!vRet) break;
                glogs_.Add(gi);

            }

With this I can bind the data to DataGridView without any problem. But I need to also get all the data to a file. either txt or any format. I tried to use serialization which I couldn't since I'm not that expert in C#. I need help as to how to write/read the data to a file. Also I though maybe there is a way to read the data from DataGridView to file but I cant do that either :). I'm a leaner.

You could use Newtonsoft to serialize the class to a file.

If you have an instance of GLogInfo object (myObj), the code would look like:

File.WriteAllText(@"c:\temp\somefile.json", JsonConvert.SerializeObject(myObj));

This would serialize all public properties in your class that have 'get' accessors.

If you wish to ignore some of the properties, you could add [JsonIgnore] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM