Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

using serialization to serialize my objects to hard drive
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


Report this thread to moderator Post Follow-up to this message
Old Post
Alexandre (www.pointnetsolutions.com)
04-23-05 08:57 PM


Re: using serialization to serialize my objects to hard drive
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..


Report this thread to moderator Post Follow-up to this message
Old Post
Alexandre (www.pointnetsolutions.com)
04-24-05 01:56 AM


Re: using serialization to serialize my objects to hard drive
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


Report this thread to moderator Post Follow-up to this message
Old Post
Rob
04-28-05 09:02 PM


Re: using serialization to serialize my objects to hard drive
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;
}
}
}


Report this thread to moderator Post Follow-up to this message
Old Post
Alexandre (www.pointnetsolutions.com)
04-29-05 09:00 PM


Re: using serialization to serialize my objects to hard drive
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


Report this thread to moderator Post Follow-up to this message
Old Post
Rob
04-29-05 09:00 PM


Re: using serialization to serialize my objects to hard drive
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.


Report this thread to moderator Post Follow-up to this message
Old Post
J
04-29-05 09:00 PM


Re: using serialization to serialize my objects to hard drive
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;
}
}
}


Report this thread to moderator Post Follow-up to this message
Old Post
Alexandre (www.pointnetsolutions.com)
05-02-05 02:07 AM


Re: using serialization to serialize my objects to hard drive
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Alexandre (www.pointnetsolutions.com)
05-11-05 09:00 PM


Re: using serialization to serialize my objects to hard drive
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Rob
05-21-05 08:56 PM


Re: using serialization to serialize my objects to hard drive
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.
>



Report this thread to moderator Post Follow-up to this message
Old Post
at
05-21-05 08:56 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C# archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:28 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.