0

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.

1 Answer 1

2

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].

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. I'm able to get the data to json file. I also tried with text but it gives a json format in txt format(obviously it has to. just messing up with it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.