Connecting Tech Pros Worldwide Forums | Help | Site Map

Problems with File manipulation

Alexandre
Guest
 
Posts: n/a
#1: Nov 17 '05
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 (Alexandre.Brisebois@gmail.com)

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.Wri te);
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream,o);

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


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Problems with File manipulation


Alexandre <alexandre.brisebois@gmail.com> wrote:[color=blue]
> 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...[/color]

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.)

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Bruce Wood
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Problems with File manipulation


Could you post the error message you're getting?

Gabriel Lozano-Morán
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Problems with File manipulation


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

"Alexandre" <alexandre.brisebois@gmail.com> wrote in message
news:1113934412.903816.266850@o13g2000cwo.googlegr oups.com...[color=blue]
> 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 (Alexandre.Brisebois@gmail.com)
>
> 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.Wri te);
> IFormatter formatter = new BinaryFormatter();
> formatter.Serialize(stream,o);
>
> stream.Close();
> }
> catch(Exception e)
> {
> throw (new Exception(dir+file + " save ->> " +
> e.Message));
> }
> }
>[/color]


Alexandre
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Problems with File manipulation


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

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Problems with File manipulation


Alexandre <alexandre.brisebois@gmail.com> wrote:[color=blue]
> 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[/color]

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.
[color=blue]
> Path.Combine <-- my object locations are working properly as they are
> what could Path.Combine make better ?[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Alexandre (www.pointnetsolutions.com)
Guest
 
Posts: n/a
#7: Nov 17 '05

re: Problems with File manipulation


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...

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#8: Nov 17 '05

re: Problems with File manipulation


Alexandre (www.pointnetsolutions.com) <alexandre.brisebois@gmail.com>
wrote:[color=blue]
> 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...[/color]

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.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Alexandre (www.pointnetsolutions.com)
Guest
 
Posts: n/a
#9: Nov 17 '05

re: Problems with File manipulation


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 ?

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#10: Nov 17 '05

re: Problems with File manipulation


Alexandre (www.pointnetsolutions.com) <alexandre.brisebois@gmail.com>
wrote:[color=blue]
> 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 ?[/color]

Well, what errors are you getting?

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Closed Thread