通过添加关键字使您的用户数据可序列化:
[Serializable]
在您的数据结构之上。加载对话框时,从磁盘加载序列化结构,离开对话框时,保存数据结构。
从样式的角度来看,您可能不应该让对话框更改数据,直到对话框关闭(如果它是模态的)。
保存:
    private bool Save(String inFileName, MyObject inObject){
        try {
            FileStream theStream = File.Open(inFileName, FileMode.Create);
            BinaryFormatter theFormatter = new BinaryFormatter();
            theFormatter.Serialize(theStream, inObject);//add it to the end there
            theStream.Dispose();
            theStream.Close();
        } catch{
            return false;
        }
        return true;
    }
装载:
    private MyObject Read(String inFileName){
        MyObject theReturn = null;
        try {
            FileStream theStream = File.Open(inFileName, FileMode.Open, FileAccess.Read);                
            BinaryFormatter theFormatter = new BinaryFormatter();
            theReturn = (CImageData)theFormatter.Deserialize(theStream);//add it to the end there
            theStream.Dispose();
            theStream.Close();
        }
        catch {
            return null;
        }
        return theReturn;
    }
您也可以在流上使用“使用”,但我认为这段代码非常简单。这也意味着您可以在 MyObject 中添加更多项目。
编辑:对于加密,您可以添加 AES 或类似的东西。这对您来说可能有点过头了,将文件保存为二进制文件可能使它可以被记事本之类的东西读取,但不容易编辑。这是关于真正加密的冗长解释:
http://msdn.microsoft.com/en-us/magazine/cc164055.aspx