Traps

I have a structure that I would like to save to a file. How do I do this

Here is my structure

Code Snippet

[StructLayout(LayoutKind.Sequential)]
private class structure
{
public string a;
public string b;
public int x;
public DateTime dt;
}

I tried using

Code Snippet
this.file.WriteLine(structure);

however all it does is put this line in a file

Code Snippet
Server.Form1+structure

My end goal is to have this data file compressed and encrypted, and of course be able to retrieve the contents programmaticly as well. Should I be using ado instead Should I be saving the structure in some kind of collection class, and then write that to a file. I've done alot of reading and its only confused me more. Some guidence in how to make this as effecient as possible would be great.



Re: .NET Base Class Library Best way to save data to a file?

Traps

I'm thinking I should use a the BinaryWriter class, not too sure how to go about it.

I tried this ,where bFile is a BinaryWriter instance.

Code Snippet

structure mystruct = new structure();

this.bFile.Write((structure)mystruct);

Doesnt work.

Ok, I think I kinda understand the BinaryWriter class a little better.

With BinaryWriter, you have to write primitive data types. You can¡¯t write out a whole structure. So, to output the information from a Part class, you need to write each field individually:

I assume primitive data types means, the like int, string, bool, etc..... So I cant just write an entire structure I have to write each individual piece.

In vb6 I used this

Code Snippet
Open sFilename For Random Access Write As iFile

For lIndex = LBound(tEventArray) To UBound(tEventArray)
Put #iFile, , tEventArray(lIndex)
Next

where tEventArray is a structure. The output file must have been binary because it was jibberish in notepad.exe. I could read the file back into the tEventArray structure and retrieve my data.

So why could I write the structure in vb6 to a binary file, but not in c#

The .NET Framework doesn¡¯t let you read entire structures like that. In general, there¡¯s no guarantee of how a class or structure will be stored internally, so reading a bunch of consecutive bytes most likely will not give you the result you were looking for.

I now understand that c# simply wont let you do it.

Well I think I've answered my own question. I'll mark this as resolved. If anyone finds error in what I have written, please comment. Or if you have a better suggestion on saving data to a file so that it is compact, and secure, let me know.

I obtained the information about BinaryWriter from here





Re: .NET Base Class Library Best way to save data to a file?

Traps

Ok, using BinaryWrite will make the data on the disk binary, however if you open the file in notepad it shows the text. This is because notepad knows how to convert the binary data. I need to take my situation a bit further and use CryptoStream. This will allow me to write the data in binary, and encrypt it at the same time.



Re: .NET Base Class Library Best way to save data to a file?

Traps

Can anyone explain the difference between this code

Code Snippet

FileStream fs = File.Open(fileName, FileMode.Create);

DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();

cryptic.Key = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");

cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ABCDEFGH");

crStream = new CryptoStream(fs, cryptic.CreateEncryptor(), CryptoStreamMode.Write);

and this code

Code Snippet

// Create or open the specified file.

FileStream fStream = File.Open(fileName, FileMode.OpenOrCreate);

// Create a new Rijndael object.

Rijndael RijndaelAlg = Rijndael.Create();

// Create a CryptoStream using the FileStream

CryptoStream cStream = new CryptoStream(fStream,

RijndaelAlg.CreateEncryptor(RijndaelAlg.Key,RijndaelAlg.IV),

CryptoStreamMode.Write);

// Create a StreamWriter using the CryptoStream.

crStream = new StreamWriter(cStream);

I understand they are using different encryptions, but the first one creates crStream as an instance of CryptoStream, while the 2nd one uses crStream as an instance of a StreamWriter. Both encrypt.





Re: .NET Base Class Library Best way to save data to a file?

Traps

Ok I learned a little about serialization. I think this is what needs to be done. Please correct me if i'm wrong, but I think I should be able to use binary serialization to save a collection class to file.



Re: .NET Base Class Library Best way to save data to a file?

Traps

What I would like to do is create a collection of this class

Code Snippet

private class structure
{
public string a;
public string b;
public int x;
public DateTime dt;
}


And save it to disk as a binary encrypted file.

Is this possible. Any suggestions. (Sorry I'm not able to do much investigation on my own right now, however if anyone has any thoughts, please let me know)