For Programmers: Free Programming Magazines  


Home > Archive > C# > May 2005 > using serialization to serialize my objects to hard drive









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author using serialization to serialize my objects to hard drive
Alexandre (www.pointnetsolutions.com)

2005-04-23, 3:57 pm

Hey im using serialization to serialize my objects to hard drive,
reading them is no problem but overwriting them causes me problems,i
have un authorized access to the files but i gave the folder all the
permissions.

source:
http://www.vkarlsen.no/pastebin/default.asp?id=5545

can someone tell me what im not doing right

Alexandre (www.pointnetsolutions.com)

2005-04-23, 8:56 pm

this only occurs when i write a new file and then try to over write it.
if the thread finishes and starts overagain i am able to overwrite a
file that already exists as often as i want..

Rob

2005-04-28, 4:02 pm

Sounds like your file is still locked from the first opperation, a code
snipit may help?

Check that at the end of your serialization that you have a finally
block that closes the file object explicitly.

Rob
http://www.enigmatechnologies.co.uk

Alexandre (www.pointnetsolutions.com)

2005-04-29, 4:00 pm

Hey rob,

here is the actual code im using ::::::



using System;
using System.IO;
using System.Threading;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Core.Utils
{
public class SerializeObject
{

public static void save(string dir,string file, object o)
{
try
{

using(Stream stream = new
FileStream(dir+file,FileMode.Create,FileAccess.Write))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream,o);

stream.Close();
}
}
catch(Exception e)
{
throw e;
}
}

public static object Load(string dir, string file)
{

object o = null;
try
{
using(Stream stream = new
FileStream(dir+file,FileMode.Open,FileAccess.Read))
{
IFormatter formatter = new BinaryFormatter();
o = formatter.Deserialize(stream);

stream.Close();
}
}
catch(Exception e)
{
return null;
}
return o;
}
}
}

Rob

2005-04-29, 4:00 pm

Hi,
When I go back to the office I will look at some more samples but
this is what I can see at the moment.

1. The Load and Save functions are inconsistent in that the save throws
and exception but the load returns null. I would try and be more
consistent.

2. As I said before if the BinaryFormatter throws an exception the
stream will be left open, use an additional try catch finally or move
the scope of the stream out and add an outer finally that call close
if the stream object has been created.

Sorry I can't be more help at the moment.

Rob

J

2005-04-29, 4:00 pm

You mention the use of threads, could one of your other threads be
attempting to read or write the file?

As for the using statement, I thought that implied a try/finally block
with a Dispose, but I could be mistaken.

Incidently, by re-throwing "e" in the save method you'll reset your
stack trace for the exception. You should either do away with the
try/catch, simply have the throw statement by itself to rethrow the
exception, or throw a new exception and pass in the caught one as the
inner exception.

J.

Alexandre (www.pointnetsolutions.com)

2005-05-01, 9:07 pm

Hey rob,

here is the actual code im using ::::::



using System;
using System.IO;
using System.Threading;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Core.Utils
{
public class SerializeObject
{

public static void save(string dir,string file, object o)
{
try
{

using(Stream stream = new
FileStream(dir+file,FileMode.Create,FileAccess.Write))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream,o);

stream.Close();
}
}
catch(Exception e)
{
throw e;
}
}

public static object Load(string dir, string file)
{

object o = null;
try
{
using(Stream stream = new
FileStream(dir+file,FileMode.Open,FileAccess.Read))
{
IFormatter formatter = new BinaryFormatter();
o = formatter.Deserialize(stream);

stream.Close();
}
}
catch(Exception e)
{
return null;
}
return o;
}
}
}

Alexandre (www.pointnetsolutions.com)

2005-05-11, 4:00 pm

for the last exception that needs to be caught if i put a try catch
over the using would i catch it properly ? doesnt the using block
automatically finalize and call dispose ?

as for the inconsistancy, i know i should be consistant but since im
trying everything i can imagine to fix the problem both my methods are
not so consistant.

Rob

2005-05-21, 3:56 pm

Yes looking at the documentation you are right the using statement will
call dispose which will implicitly call close. Still haven't quite got
used to using all of the language constructs to there full advantage.

at

2005-05-21, 3:56 pm

Won't assigning null to the instance after use speed up the disposal
process?

> "Rob" <google@enigmatechnologies.co.uk> wrote in message
> news:1116675911.640198.43900@g44g2000cwa.googlegroups.com...
> Yes looking at the documentation you are right the using statement will
> call dispose which will implicitly call close. Still haven't quite got
> used to using all of the language constructs to there full advantage.
>



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com