Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Serialization alternatives/options.

I'm looking into creating an email client with C#. I want all the data stored in one file, but don't want to have to use a seperate data source (Access DB, SqlServer, etc).  I'm leary about using the standard serialization, because it would seem that everytime something is changed (new messages, message moved, etc), i would have to rewrite the file (i may be wrong on this).  In the event that say, new mail has been retrieved, and the program was closed abnormally (prior to a save), these new messages would be lost.  I liked the idea of the IStorage interface in C++, and was wondering if there was any equivalent in C#. I also looked into using XML, but this would seem to store everything in plain text.  Can anyone offer any insights into any possible storage options?

Thanks,
Joe Krill
0
jkrill
Asked:
jkrill
  • 3
  • 2
1 Solution
 
PickleCommented:
If you are looking to store emails, XML would be an ideal storage medium.

1. All email is plaintext, even attachements are UU or BinHex encoded to plaintext anyways.

2. All of the .NET data access components are backended by XML so you would still be able to use DataSets, DataTables, DataRows, etc...

I would hope that you don't expect your program to just die without any notice or chance to write to disk.  The only time I could see this happening is if they shut down the computer without closing your program.  But even MS Outlook will not let you shutdown your computer without first closing it.

If you just make sure that in the destructor of your main form or one of your main objects that you write the DataSet out to disk you should be just fine.  That destructor will still get called in the instance of almost any error or problem that your application may encounter.

Let me know if you have any, more-specific questions.
0
 
jkrillAuthor Commented:
XML would be perfect, except for the fact that I don't want the data stored in plain text on the disk.  Is XML capable of this?
0
 
PickleCommented:
Just do this to encrypt the xml as you write it out to disk:
     //Create the file stream to handle the output file.
     FileStream fout = new FileStream(@"C:\mailstore.mls", FileMode.OpenOrCreate, FileAccess.Write);

     RijndaelManaged rm = new RijndaelManaged();
     
     byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
     byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};

     //Create a CryptoStream, pass it the NetworkStream, and encrypt it with the Rijndael class.
     CryptoStream encStream = new CryptoStream(NetStream, rm.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
 
     myDataSet.WriteXml(encStream);
 
     encStream.Close();  
     fout.Close();

You can just do this backwards to pull it back into a DataSet...

If you want more info on the Key and IV look up RijndaelManaged in the Visual Studio help.
0
 
PickleCommented:
Sorry, screwed up on one of the lines, I was using this code previously to encrypt data over a network connection.  This should work:

   //Create the file stream to handle the output file.
    FileStream fout = new FileStream(@"C:\mailstore.mls", FileMode.OpenOrCreate, FileAccess.Write);

    RijndaelManaged rm = new RijndaelManaged();
   
    byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
    byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};

    //Create a CryptoStream, pass it the FileStream, and encrypt it with the Rijndael class.
    CryptoStream encStream = new CryptoStream(fout, rm.CreateEncryptor(Key, IV), CryptoStreamMode.Write);

    myDataSet.WriteXml(encStream);

    encStream.Close();  
    fout.Close();
0
 
jkrillAuthor Commented:
Perfect - exactly what I was looking for.  Thank You!
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now