Problems with File manipulation

A

Alexandre

Hey im serializing objects to files am i forgeting something
it works great but once they are written i dont have access to deleting
them or overwriting them..
my folder properties are everyone can update delete and read and write
i set everything to everyone... juste to be sure.
and the owner of the file is the application is question...

heres my code

Alexandre ([email protected])

public static void save(string dir,string file, object o)
{
try
{
if(File.Exists(dir+file))
{
File.Delete(dir+file);
}
Stream stream = new
FileStream(dir+file,FileMode.Create,FileAccess.Write);
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream,o);

stream.Close();
}
catch(Exception e)
{
throw (new Exception(dir+file + " save ->> " +
e.Message));
}
}
 
J

Jon Skeet [C# MVP]

Alexandre said:
Hey im serializing objects to files am i forgeting something
it works great but once they are written i dont have access to deleting
them or overwriting them..
my folder properties are everyone can update delete and read and write
i set everything to everyone... juste to be sure.
and the owner of the file is the application is question...

Could you post a short but *complete* program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that you should use a using statement with your stream, so that
even if the formatting fails, you still close the file.

(You should also use Path.Combine to go from directory and filename to
fully qualified filename.)
 
G

Gabriel Lozano-Morán

Can you also give an example of a class marked with serializable attribute
and an example of how you call the static save() method. I have copy+pasted
your code and I have created a small test class and it works fine here.

Gabriel Lozano-Morán
Software Engineer
Sogeti
 
A

Alexandre

since i rebuilt my application,
webservice and web application and assembly

and since i also disconnected from my ftp (to the webservice folder)

the access denied has stoped and the service runs exacly has it
should... for some odd reason,

what i was doing was saving locally a serialized version of the user
object so when i update user information it had an access denied on the
serialization of the object and on the delete of the object, in turn it
was not removing the ram object from the hash map that contained it.

when retreiving an object this is what i do,

check ram --> check hard drive --> fetch from sql db

so im stopping as many requests from reaching the database and im aslo
limiting the creation of new objects...

Jon mentioned "using" ive seen it alot but never understood how to use
it, i know what it does i just dont know how ... exactly

Path.Combine <-- my object locations are working properly as they are
what could Path.Combine make better ?

Alexandre
 
J

Jon Skeet [C# MVP]

Alexandre said:
Jon mentioned "using" ive seen it alot but never understood how to use
it, i know what it does i just dont know how ... exactly

It basically inserts a try/finally block into your code automatically,
calling Dispose at the end of the block. It's much easier to get
reliable code which properly disposes of resources in the face of
exceptions that way.
Path.Combine <-- my object locations are working properly as they are
what could Path.Combine make better ?

It would mean that you wouldn't need to make sure you had a directory
separator at the end of one of your strings, for one thing.
 
A

Alexandre (www.pointnetsolutions.com)

thats interesting, at the moment the separator is an integral part of
my app its handeled by the app its self... since im doing a
server.mappath and a couple things in my auto manipulation... but i
will heep in mind your comments...

i need to find a good example of using(object) { .. } because every
example ive seen got me a bit confused so far...
 
J

Jon Skeet [C# MVP]

Alexandre (www.pointnetsolutions.com) said:
thats interesting, at the moment the separator is an integral part of
my app its handeled by the app its self... since im doing a
server.mappath and a couple things in my auto manipulation... but i
will heep in mind your comments...

i need to find a good example of using(object) { .. } because every
example ive seen got me a bit confused so far...

Well, in the code you've got, you'd change it to:

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

then even if formatting throws an exception, you'll still close the
stream.
 
A

Alexandre (www.pointnetsolutions.com)

hey Jon,

i tryed changing what you told me, but for some odd reasion it does not
work,
from time to time it stops working and i get errors again ..

any thoughts ?
 
J

Jon Skeet [C# MVP]

Alexandre (www.pointnetsolutions.com) said:
i tryed changing what you told me, but for some odd reasion it does not
work,
from time to time it stops working and i get errors again ..

any thoughts ?

Well, what errors are you getting?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top