开发者

Write out to a file a dictionary of objects and string

开发者 https://www.devze.com 2023-01-26 22:31 出处:网络
Trying to run through a dictionarystring and write the contents to file that should be easy on the eyes.

Trying to run through a dictionary string and write the contents to file that should be easy on the eyes.

 Dictionary<CArray, string> dictCArray = new Dictionary<CArray, string>();
            DateTime dtmPad = DateTime.Today;
            for (int i = 0; i < s1.Length / 2; i++)
            {
                CArray caobject = new CArray();
                dictCArray.Add(caobject, s1[i, 0]);
                for (int x = 0; x < idatelength; x++)
                {
                    caobject.dtmday = dtmPad.AddDays(x);
                    caobject.s2 = Create(ilength);
                    caobject.s3 = s1[i, 1];
                    caobject.dictC.Add(caobject.dtmday, caobject.s2);
                }
            }

Where CArray is a class with two strings and a dictionary that contains a datetime and a string.

class CArray
{
    public DateTime dtmday;
    public string s2;
    public string s3;
    public Dictionary<DateTime, string> dictC = new Dictionary<DateTime, string>();
}

I need a way to run through it and output it to a file, and also have the ability to read that file back into the same开发者_C百科 format. I am just stumped on how to do it. Any help would be appreciated.


You should look into serialization. All you'll need to do is implement two functions for your custom class.

A note: This won't make easy-to-read files, but it will work well for saving/loading from files.


You could loop through the varying types in a linear fashion and make use of the BinaryWriter. To read the data back in you would use the BinaryReader.

You could write out out the DateTime as a long via the Ticks property.

Here is a brief sample on writing/reading with the BinaryWriter and BinaryReader and a Dictionary<K,V>

0

精彩评论

暂无评论...
验证码 换一张
取 消