Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

storing important data locally

What's a good way to store important data to a windows app?  By important data, I'm talking about things like database connection strings, paths to files, usernames and passwords, etc.  Is the registry a good choice?

rick
Thursday, December 05, 2002

You could use an AppName.exe.config file and just store your connection strings in the <appSettings> section.

sean slavin
Thursday, December 05, 2002

You mentioned passwords. That makes both the registry and configuration files lousy choices, unless the passwords are just a nuisance factor rather than a security issue.

For higher-security storage, you might look into the cryptography classes.

But bottom line: storing secrets on the computer is just a lousy idea in most cases.

Mike Gunderloy
Thursday, December 05, 2002

Storing a connection string to my sql server wouldn't be a good idea either, as it would contain my username/pass.

So do most simple programs (FTP or mail clients for example) store your saved settings in an encrypted file? 

rick
Thursday, December 05, 2002

Storing connection strings locally is acceptable, but you shouldn't store passwords anyway. User should enter password on every connection to database. As an alternative solution you can use "Integrated Security=true" in your connection string. In this case user should enter password only to logon to Windows.

Mikhail Andronov
Friday, December 06, 2002

And to return to the original question - assuming that you're only storing things that you ought to store <g>, I would take a look at Isolated Storage. This provides a single API across all the MS operating systems for per-user storage of data. Here's one way to use it. First, collect all of your application's settings into a single serializable class:

<Serializable()> _
Public Class Settings
    Public Debug As Boolean
    Public Argb As Integer
End Class

In this case, I'm persisting some settings from a form. To save the settings to isolated storage, I use code in the Closing event:

    Private Sub frmSettings_Closing( _
    ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs) _
    Handles MyBase.Closing
        BreakIfChecked(Me.chkDebug)
        Try
            ' Get the isolated store for this assembly
            Dim isf As IsolatedStorageFile = _
            IsolatedStorageFile.GetUserStoreForAssembly()

            ' Create or truncate the settings file
            Dim isfs1 As IsolatedStorageFileStream = _
            New IsolatedStorageFileStream("Settings.xml", _
            FileMode.Create, FileAccess.Write, isf)

            ' Construct a settings object
            Dim s As Settings = New Settings()
            s.Debug = Me.chkDebug.Checked
            s.Argb = Me.BackColor.ToArgb()

            ' Serialize the object to the file
            Dim xs As XmlSerializer = _
            New XmlSerializer(GetType(Settings))
            xs.Serialize(isfs1, s)
            isfs1.Close()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error")
        End Try
    End Sub

To load the settings, here's the corresponding code from the Load event:

    Private Sub frmSettings_Load( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        BreakIfChecked(Me.chkDebug)
        Try
            ' Get the isolated store for this assembly
            Dim isf As IsolatedStorageFile = _
            IsolatedStorageFile.GetUserStoreForAssembly()

            ' Open the settings file
            Dim isfs1 As IsolatedStorageFileStream = _
            New IsolatedStorageFileStream("Settings.xml", _
            FileMode.Open, FileAccess.Read, isf)

            ' Deserialize the XML to a settings object
            Dim s As Settings = New Settings()
            Dim xtr As XmlTextReader = New XmlTextReader(isfs1)
            Dim xs As XmlSerializer = _
            New XmlSerializer(GetType(Settings))
            s = CType(xs.Deserialize(xtr), Settings)

            ' And apply the settings to the form
            Me.chkDebug.Checked = s.Debug
            Me.BackColor = Color.FromArgb(s.Argb)

            isfs1.Close()
        Catch ex As Exception
            ' No file found. Just run with the existing settings
        End Try
    End Sub

Mike Gunderloy
Friday, December 06, 2002

*  Recent Topics

*  Fog Creek Home