Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messagethis 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..
Post Follow-up to this messageSounds 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
Post Follow-up to this messageHey 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;
}
}
}
Post Follow-up to this messageHi, 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
Post Follow-up to this messageYou 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.
Post Follow-up to this messageHey 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;
}
}
}
Post Follow-up to this messagefor 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.
Post Follow-up to this messageYes 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.
Post Follow-up to this messageWon'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. >
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.