autosave

autosave

Old forum URL: forums.lhotka.net/forums/t/3817.aspx


mtagliaf posted on Wednesday, October 31, 2007

I would like to implement an Autosave capability into my CSLA application. I currently intend to use the BinaryFormatter to serialize the object to an Autosave folder. This will be hooked into the CSLA IsDirtyChanged event, I think.

When the object is saved to the database, I will delete the autosave file from disk.

When the application starts, I will check the Autosave folder, and "rehydrate" any files found there back into CSLA objects.

I have read about the potential versioning problems with the BinaryFormatter - I intend to ignore these issues for now - as the purpose of the autosaver is short-term storage only. Any exceptions I get in the
"rehydrate" process I will treat as failed Autosaves.

Does anyone see any other potential issues with this approach, or, better yet, has anyone ever done it and has code they would like to share? <g>

matt tag




JoeFallon1 replied on Wednesday, October 31, 2007

Matt,

I wrote some code for this about 4 years ago.

Here the class that is saved to disk:

<Serializable()> Public Class StartUpValues
Public Server As String = "jfallon"
Public DataBase As String = "myDB"
Public Uid As String = "Joe"
<NonSerialized()>
Public Pwd As String = "fallon"
Public ConnectionString As String = "someConnString"
Public DBtype As String = "SQL Server"
End Class

Here is how I saved it as a "fake" encrytped .dll file:

Private Sub SaveValues()
With objStartUp
  .Server = Me.txtServer.Text
  .DataBase =
Me.txtDatabase.Text
  .Uid = Me.txtName.Text
  .Pwd =
Me.txtPassword.Text
  .ConnectionString = connString
  .DBtype = DBtype
End With

Dim CurDir As String
CurDir = Environment.CurrentDirectory & "\"
Dim myFileStream As Stream = File.Create(CurDir & "plainFileName.dll")
Dim serializer As New BinaryFormatter
serializer.Serialize(myFileStream, objStartUp)
myFileStream.Close()

If File.Exists(CurDir & "plainFileName.dll") Then
  Dim objCrypto As New Crypto
  objCrypto.EncryptFile(CurDir & "plainFileName.dll", CurDir & "FileName.dll")
  File.Delete(CurDir & "plainFileName.dll")
End If

End Sub

Then in a Startup form load method I have code like this to decrypt the file and read it:

Private objStartUp As StartUpValues

objStartUp = GetStartUpValues()

Public Function GetStartUpValues() As StartUpValues
 
Dim objStartUp As StartUpValues
 
 
If File.Exists("FileName.dll") Then
   
Dim CurDir As String
   
CurDir = Environment.CurrentDirectory & "\"

   
Dim objCrypto As New Crypto
    objCrypto.DecryptFile(CurDir & "FileName.dll", CurDir & "plainFileName.dll")
    objCrypto =
Nothing

   
Dim myFileStream As Stream = File.OpenRead("plainFileName.dll")
   
Dim deserializer As New BinaryFormatter
    objStartUp =
CType(deserializer.Deserialize(myFileStream), StartUpValues)
    myFileStream.Close()
    File.Delete(CurDir & "plainFileName.dll")
 
End If

 
Return objStartUp
End Function

 

Joe

tiago replied on Wednesday, October 31, 2007

JoeFallon1:

Matt,

I wrote some code for this about 4 years ago.

Here the class that is saved to disk:

Hi Joe Fallon,

I find this subject very interesting but my knowledge of VB is nil. I usually convert VB files to C# using SharpDevelop. But this only works if the VB source compiles all right. Using your post I made up the following files:

StartUpValues.vb

<Serializable()> Public Class StartUpValues
  Public Server As String =
"jfallon"
  Public DataBase As String =
"myDB"
  Public Uid As String =
"Joe"
  <NonSerialized()> Public Pwd As String =
"fallon"
  Public ConnectionString As String =
"someConnString"
  Public DBtype As String =
"SQL Server"
End Class

StartupForm.vb

Imports System.IO
Imports
System.Runtime.Serialization.Formatters.Binary

  Public Class
StartupForm
  '
  'Then in a Startup form load method I have code like this to decrypt the
  'file and read it:
 
'
  Private objStartUp As
StartUpValues
  objStartUp = GetStartUpValues()

  Public Function GetStartUpValues() As
StartUpValues
    Dim objStartUp As
StartUpValues

    If File.Exists("FileName.dll")
Then

      Dim CurDir As
String
      CurDir = Environment.CurrentDirectory &
"\"

      Dim objCrypto As New
Crypto
      objCrypto.DecryptFile(CurDir &
"FileName.dll", CurDir & "plainFileName.dll"
)
      objCrypto =
Nothing

      Dim myFileStream As Stream = File.OpenRead("plainFileName.dll"
)
      Dim deserializer As New
BinaryFormatter
      objStartUp =
CType
(deserializer.Deserialize(myFileStream), StartUpValues)
      myFileStream.Close()
      File.Delete(CurDir &
"plainFileName.dll"
)
    End
If
    Return
objStartUp
  End
Function
  '
  'Here is how I saved it as a "fake" encrytped .dll file:
  '
  Private Sub
SaveValues()
    With
StartupForm.objStartUp
      .Server =
Me
.txtServer.Text
      .DataBase =
Me
.txtDatabase.Text
      .Uid =
Me
.txtName.Text
      .Pwd =
Me
.txtPassword.Text
      .ConnectionString = connString
      .DBtype = DbType
    End
With

    Dim CurDir As
String
    CurDir = Environment.CurrentDirectory &
"\"
    Dim myFileStream As Stream = File.Create(CurDir & "plainFileName.dll"
)
    Dim serializer As New
BinaryFormatter
    serializer.Serialize(myFileStream, objStartUp)
    myFileStream.Close()

    If File.Exists(CurDir & "plainFileName.dll")
Then
      Dim objCrypto As New
Crypto
      objCrypto.EncryptFile(CurDir &
"plainFileName.dll", CurDir & "FileName.dll"
)
      File.Delete(CurDir &
"plainFileName.dll"
)
    End
If
  End
Sub

End
Class

StartupForm.vb reports too many errors. Can you help me?

TIA

Pawz replied on Wednesday, October 31, 2007

I'd recommend finding & using an online code converter - VB -> C# is pretty simple though, if you just take a bit to learn the differences.
VB C# Comparison is one of my favorite references, although I came from VB to C#.

In any case, there's a converter you can use to convert code snippets here (I'm sure there's more, just noticed this on Telerik's site...):

http://www.codechanger.com/



With a bit of fiddling, I got this, although it's not quite right (converter doesn't seem to like the 'with' keyword in VB)

[Serializable()]
public class StartUpValues
{
    public string Server = "jfallon";
    public string DataBase = "myDB";
    public string Uid = "Joe";
    [NonSerialized()]
    public string Pwd = "fallon";
    public string ConnectionString = "someConnString";
    public string DBtype = "SQL Server";
}

public class Convert
{
    private void SaveValues()
    {
         // ERROR: Not supported in C#: WithStatement


        string CurDir;
        CurDir = Environment.CurrentDirectory + "\\";
        Stream myFileStream = File.Create(CurDir + "plainFileName.dll");
        BinaryFormatter serializer = new BinaryFormatter();
        serializer.Serialize(myFileStream, objStartUp);
        myFileStream.Close();

        if (File.Exists(CurDir + "plainFileName.dll"))
        {
            Crypto objCrypto = new Crypto();
            objCrypto.EncryptFile(CurDir + "plainFileName.dll", CurDir + "FileName.dll");
            File.Delete(CurDir + "plainFileName.dll");
        }

    }

    private StartUpValues objStartUp;

     objStartUp = GetStartUpValues();

    public StartUpValues GetStartUpValues()
    {
        StartUpValues objStartUp;

        if (File.Exists("FileName.dll"))
        {
            string CurDir;
            CurDir = Environment.CurrentDirectory + "\\";

            Crypto objCrypto = new Crypto();
            objCrypto.DecryptFile(CurDir + "FileName.dll", CurDir + "plainFileName.dll");
            objCrypto = null;

            Stream myFileStream = File.OpenRead("plainFileName.dll");
            BinaryFormatter deserializer = new BinaryFormatter();
            objStartUp = (StartUpValues)deserializer.Deserialize(myFileStream);
            myFileStream.Close();
            File.Delete(CurDir + "plainFileName.dll");
        }

        return objStartUp;
    }
}

//=======================================================
//Service provided by Telerik (www.telerik.com)
//Conversion powered by NRefactory.
//Built and maintained by Todd Anglin and Telerik
//=======================================================


JoeFallon1 replied on Thursday, November 01, 2007

It is sample code.

There is no Crypto class in the .Net framework - it is one of my utility classes that uses the .Net version. You should probably comment it out.

The idea was simply to write the file to disk, call it a .dll in case someone was poking around and if they opened it they would see "junk".

If all you want to do is save and retrieve values you do not need encryption.

Joe

 

Copyright (c) Marimer LLC