文件操作问题 [英] Problems with File manipulation

查看:62
本文介绍了文件操作问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿我正在将对象序列化为文件我伪造的东西

它的效果很好但是一旦它们被编写,我就无法删除

或覆盖它们。

我的文件夹属性是每个人都可以更新删除和读写

i设置一切给大家... juste肯定。

和所有者该文件是应用程序的问题...


继承人我的代码


Alexandre(Al *********) ********@gmail.com)

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

{

尝试

{

if(File.Exists(dir + file))

{

File.Delete(dir + file);

}

流stream = new

FileStream(dir + file,FileMode.Create, FileAccess.Wri te);

IFormatter formatter = new BinaryFormatter();

formatter.Serialize(stream,o);


stream.Close();

}

catch(例外e)

{

throw(新例外(dir + file +"保存 - >> " +

e.Message));

}

}

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 (Al*****************@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));
}
}

推荐答案

Alexandre< al ***************** @ gmail.com>写道:
Alexandre <al*****************@gmail.com> wrote:
嘿我将对象序列化为文件我伪造的东西
它的效果很好但是一旦它们被写入,我就无法删除它们或覆盖它们。
我的文件夹属性是每个人都可以更新删除和读写
我将所有内容设置为每个人... juste确定。
和文件的所有者是应用程序是问题...
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...




你能发一个简短的*完整*程序来演示

的问题吗?


请参阅 http://www.pobox.com/~skeet/csharp /complete.html 了解详情

我的意思是什么。


请注意,你应该在你的流中使用using语句,所以那个

即使格式化失败,你仍然关闭文件。


(你还应该使用Path.Combine从目录和文件名转到

已满y限定文件名。)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复对于小组,请不要给我发邮件



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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


你能发布你收到的错误信息吗?

Could you post the error message you''re getting?


您是否还可以给出一个标有serializable属性的类的示例

以及如何调用static save()方法的示例。我已经复制+粘贴了

您的代码我已经创建了一个小测试类,它在这里工作正常。


Gabriel Lozano-Morán

软件工程师

Sogeti


" Alexandre" <人***************** @ gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...
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" <al*****************@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
嘿我将对象序列化为文件我伪造的东西
它的效果很好但是一旦它们被编写我就无权删除它们或覆盖它们。
我的文件夹属性是每个人都可以更新删除和读写
我把所有东西都设置给每个人...这是肯定的。
和文件的所有者是应用程序是问题...
继承人我的代码

亚历山大(Al*****************@gmail.com)

public static void save( string dir,string file,object o)
{
尝试
{
if(File.Exists(dir + file))
{
文件。删除(dir +文件);
}
流stream = new
FileStream(dir + file,FileMode.Create,FileAccess.Wri te);
IFormatter formatter = new BinaryFormatter( );
formatter.Serialize(stream,o);

stream.Close();
}
catch(例外e)
{
throw(new Exception(dir + file +"保存 - >> " +
e.Message));
}
}
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 (Al*****************@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));
}
}



这篇关于文件操作问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆