locked
Problem with file size in Serisalization RRS feed

  • Question

  • Hi, I'm having a file size problem in serialization.

    i'm serializing a Dictionary.

    I serialized Dictionary with 3 item and file size become 9kb,

    i added one more item and serialized then file size became 30kb,

    i deleted last one item and the serialized again but the filesize remained 30 kb. Why is this happening?

    my code for serialization

    public static void writeFeed(Dictionary<string, List<Feed>> fd)
            {
                Stream _stream = null;
                try
                {
                    _stream = File.OpenWrite("Feeds.bin");
                    BinaryFormatter bf = new BinaryFormatter();
                    //List<Feed> fds = getsetFeed.readFeed();
                    //fds.Add(fd);
                    bf.Serialize(_stream, fd);
                }
                catch (Exception x)
                {
                    throw;
                }
            }


    Regards Kumar Gaurav.
    Wednesday, December 7, 2011 11:44 AM

Answers

  • Because when you do OpenWrite() with an existing file, it begins writing from the beginning but it does NOT MAKE THE FILE SMALLER.

    So what happened is the file grew to 30KB, then when you rewrote it with smaller data it wrote out 9KB and left 21KB of the old data at the end.

    • Marked as answer by Leo Liu - MSFT Wednesday, December 14, 2011 5:58 AM
    Wednesday, December 7, 2011 12:24 PM
  • Hi urprob,

    Here everytime we should first clear all the existing content of the file and then write to the file:
            public static void writeFeed(Dictionary<string, List<string>> fd)
            {
                Stream _stream = null;
                try
                {
                    //Clear first.
                    File.WriteAllBytes(@"C:\Users\v-xzliu\Desktop\Feeds.bin", new byte[] { });
    
                    _stream = File.OpenWrite(@"C:\Users\v-xzliu\Desktop\Feeds.bin");
                    BinaryFormatter bf = new BinaryFormatter();
                    //List<Feed> fds = getsetFeed.readFeed();
                    //fds.Add(fd);     
                    bf.Serialize(_stream, fd);
                }
                catch (Exception x)
                {
                    throw;
                }
            }
    

    Have a nice day,
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Leo Liu - MSFT Wednesday, December 14, 2011 5:58 AM
    Thursday, December 8, 2011 5:27 AM

All replies

  • Because when you do OpenWrite() with an existing file, it begins writing from the beginning but it does NOT MAKE THE FILE SMALLER.

    So what happened is the file grew to 30KB, then when you rewrote it with smaller data it wrote out 9KB and left 21KB of the old data at the end.

    • Marked as answer by Leo Liu - MSFT Wednesday, December 14, 2011 5:58 AM
    Wednesday, December 7, 2011 12:24 PM
  • Hi urprob,

    Here everytime we should first clear all the existing content of the file and then write to the file:
            public static void writeFeed(Dictionary<string, List<string>> fd)
            {
                Stream _stream = null;
                try
                {
                    //Clear first.
                    File.WriteAllBytes(@"C:\Users\v-xzliu\Desktop\Feeds.bin", new byte[] { });
    
                    _stream = File.OpenWrite(@"C:\Users\v-xzliu\Desktop\Feeds.bin");
                    BinaryFormatter bf = new BinaryFormatter();
                    //List<Feed> fds = getsetFeed.readFeed();
                    //fds.Add(fd);     
                    bf.Serialize(_stream, fd);
                }
                catch (Exception x)
                {
                    throw;
                }
            }
    

    Have a nice day,
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    • Marked as answer by Leo Liu - MSFT Wednesday, December 14, 2011 5:58 AM
    Thursday, December 8, 2011 5:27 AM
  • Hi urprob,

    How is it going now with our friends' suggestions?
    We are looking forward to hearing from you.

    Have a nice day,
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Monday, December 12, 2011 5:38 AM