That article has an section "Save Settings" that shows one way of saving
settings to disk:
http://msdn.microsoft.com/library/de...un03282003.asp
************************************************** **********************
Here is some code I've gathered that uses Isolated Storage:
Imports System.IO
Imports System.IO.IsolatedStorage
Imports Microsoft.Win32
Public Class UserSettings
Private Shared Function _
GetIEDefaultSaveLocation() As String
'When you download a file from IE,
'it defaults to the last location
'you downloaded a file to, this code
'just pulls that location out of the
'registry. If the setting isn't available,
'the user's My Documents folder is used
'as a default.
Dim saveDir As String
Try
Dim IEMain As RegistryKey
IEMain = Registry.CurrentUser.OpenSubKey _
("Software\Microsoft\Internet Explorer\Main", _
False)
saveDir = IEMain.GetValue("Save Directory", _
Nothing)
If saveDir Is Nothing Then
'default to user's My Documents folder
saveDir = Environment.GetFolderPath( _
Environment.SpecialFolder.Personal)
End If
Catch ex As Exception
MsgBox(ex.ToString)
saveDir = Directory.GetDirectoryRoot( _
Application.StartupPath)
End Try
Return saveDir
End Function
Public Shared Function GetSettings() _
As Options
Try
Dim m_Options As Options
Dim settingsPath As String = "settings.xml"
Dim isf As IsolatedStorageFile
isf = IsolatedStorageFile.GetUserStoreForAssembly
If isf.GetFileNames(settingsPath).Length > 0 Then
Dim myXMLSerializer As New _
Xml.Serialization.XmlSerializer( _
GetType(Options))
m_Options = CType( _
myXMLSerializer.Deserialize( _
New IsolatedStorageFileStream _
(settingsPath, IO.FileMode.Open, _
IO.FileAccess.Read)), Options)
Else
m_Options = New Options()
m_Options.defaultSaveLocation = _
GetIEDefaultSaveLocation()
End If
Debug.WriteLine(m_Options.defaultSaveLocation)
Return m_Options
Catch ex As System.Exception
MsgBox(ex.ToString)
Return Nothing
End Try
End Function
Public Shared Sub SaveSettings( _
ByVal currentSettings As Options)
Try
Dim isf As IsolatedStorageFile
isf = IsolatedStorageFile.GetUserStoreForAssembly
Dim settingsPath As String = "settings.xml"
Dim myXMLSerializer As _
New Xml.Serialization.XmlSerializer( _
GetType(Options))
myXMLSerializer.Serialize( _
New IsolatedStorageFileStream( _
settingsPath, IO.FileMode.Create, _
IO.FileAccess.ReadWrite), _
currentSettings)
Catch ex As System.Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
<Serializable()> Public Class Options
Dim m_defaultSaveLocation As String
Public Property defaultSaveLocation() As String
Get
Return m_defaultSaveLocation
End Get
Set(ByVal Value As String)
Try
If Path.IsPathRooted(Value) AndAlso _
Not Path.HasExtension(Value) Then
If Not Directory.Exists(Value) Then
Directory.CreateDirectory(Value)
End If
m_defaultSaveLocation = Value
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Set
End Property
End Class
************************************************** ************
Here is another method that just serializes a class to disk:
Public Shared Function LoadSettings() As UserSettings
Dim mySettings As New UserSettings
Dim s As String =
Environment.GetFolderPath(System.Environment.Speci alFolder.ApplicationData)
s &= "\my program\x.bin"
If File.Exists(s) Then
Dim streamOut As FileStream
Try
Dim formatter As New BinaryFormatter
streamOut = New FileStream(s, FileMode.Open,
FileAccess.Read)
mySettings = CType(formatter.Deserialize(streamOut),
UserSettings)
Catch
Finally
streamOut.Close()
End Try
End If
Return mySettings
End Function
Public Shared Sub SaveSettings(ByVal mySettings As UserSettings)
Dim s As String =
Environment.GetFolderPath(System.Environment.Speci alFolder.ApplicationData)
s &= "\my program"
If Not Directory.Exists(s) Then
Directory.CreateDirectory(s)
End If
s &= "\x.bin"
Dim formatter As New BinaryFormatter
Dim streamIn As New FileStream(s, FileMode.Create,
FileAccess.Write)
formatter.Serialize(streamIn, mySettings)
streamIn.Close()
End Sub
<Serializable()> _
Public Class UserSettings
Private _username As String
Public Property UserName() As String
Get
If _username = Nothing Then
Return CurrentUser()
Else
Return _username.ToLower
End If
End Get
Set(ByVal Value As String)
_username = Value.ToLower
End Set
End Property
Private Function CurrentUser() As String
Dim sCurrentuser As String =
System.Security.Principal.WindowsIdentity.GetCurre nt.Name.ToLower
Dim i As Integer = sCurrentuser.IndexOf("\") + 1 ' zero based
sCurrentuser = Mid$(sCurrentuser, i + 1).ToLower
Return sCurrentuser
End Function
End Class
Hopefully that will get you going in the right direction,
Greg
"Phil Kelly" <ph********@infatech.com> wrote in message
news:cg**********@sparta.btinternet.com...
Hi
I need to write the contents of a structure to a binary file - there is
one string and 2 integers, but I can't seem to figure out how to write the
data correctly.
If I am simply writing text to a file there is no problem - that starts
when I attempt to write the structure.
Can someone help me out, please?
Thanks
Phil