By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,065 Members | 1,560 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,065 IT Pros & Developers. It's quick & easy.

writing a structure to a binary file

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
* "Phil Kelly" <ph********@infatech.com> scripsit:
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.


'FileOpen', 'FileClose', 'FilePut', 'FileGet'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2

P: n/a
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

Nov 21 '05 #3

P: n/a
"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?


Phil,

Take a look at the BinaryFormatter class - it might
do what you want.

Here's some code that writes an object to a file:

Dim obj as Object 'to write to file

'populate object

Dim formatter As BinaryFormatter = New BinaryFormatter()
Dim fs As FileStream

fs = File.Open(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)
formatter.Serialize(fs, obj)

Here's some code that reads the object from a file:

Dim obj as Object 'to be read from file
Dim formatter As BinaryFormatter
Dim fs As FileStream

fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read)
obj = formatter.Deserialize(fs)
You need to add a Serialized attribute to the object first,
though.

--
Akin

aknak at aksoto dot idps dot co dot uk
Nov 21 '05 #4

P: n/a
I tried using serialization on a structure before and I couldn't get the
structure to accept the serialization attribute. Maybe I was using the wrong
syntax:

public <serializable> mystruct as structure
public i as integer
public a as string
end structure

The compiler took it but when executed, I kept getting errors. Do you have
an example of how to make a structure serializable?

"Wild Wind" wrote:
"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?


Phil,

Take a look at the BinaryFormatter class - it might
do what you want.

Here's some code that writes an object to a file:

Dim obj as Object 'to write to file

'populate object

Dim formatter As BinaryFormatter = New BinaryFormatter()
Dim fs As FileStream

fs = File.Open(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)
formatter.Serialize(fs, obj)

Here's some code that reads the object from a file:

Dim obj as Object 'to be read from file
Dim formatter As BinaryFormatter
Dim fs As FileStream

fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read)
obj = formatter.Deserialize(fs)
You need to add a Serialized attribute to the object first,
though.

--
Akin

aknak at aksoto dot idps dot co dot uk

Nov 21 '05 #5

P: n/a
Dennis,

Try this:

<Serializable()> Public mystruct As Structure
Public i As Integer
Public a As String
End Structure

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:18**********************************@microsof t.com...
I tried using serialization on a structure before and I couldn't get the
structure to accept the serialization attribute. Maybe I was using the wrong syntax:

public <serializable> mystruct as structure
public i as integer
public a as string
end structure

The compiler took it but when executed, I kept getting errors. Do you have an example of how to make a structure serializable?

"Wild Wind" wrote:
"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?


Phil,

Take a look at the BinaryFormatter class - it might
do what you want.

Here's some code that writes an object to a file:

Dim obj as Object 'to write to file

'populate object

Dim formatter As BinaryFormatter = New BinaryFormatter()
Dim fs As FileStream

fs = File.Open(destinationFile, FileMode.OpenOrCreate, FileAccess.Write)
formatter.Serialize(fs, obj)

Here's some code that reads the object from a file:

Dim obj as Object 'to be read from file
Dim formatter As BinaryFormatter
Dim fs As FileStream

fs = File.Open(sourceFile, FileMode.Open, FileAccess.Read)
obj = formatter.Deserialize(fs)
You need to add a Serialized attribute to the object first,
though.

--
Akin

aknak at aksoto dot idps dot co dot uk

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.