无法写入新创建的文件 - 另一个进程正在使用的文件 [英] can't write to newly created file - file being used by another process

查看:110
本文介绍了无法写入新创建的文件 - 另一个进程正在使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个错误''进程无法访问该文件,因为它是另一个进程使用的
。''在我创建了一个新文件后写了然后尝试

用xmlTextWriter打开它。

FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);


bool bFileExists = oFileInfo。存在;


if(!bFileExists)


{


oFileInfo.Create();


}


oFileInfo = null;


bankWriter = new XmlTextWriter(STR_FILE_NAME,null) ; //在这里疯狂


我已经尝试将oFileInfo设置为null希望释放resourece,但

它仍然被锁定。有谁知道怎么解决这个问题?


tks

i''m getting an error about ''the process cannot access the file becaise it is
being used by another process.'' write after i''ve created a new file and try
to open it for writing with xmlTextWriter.
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if (!bFileExists)

{

oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME, null); // craps out here

i''ve tried setting oFileInfo to null hoping to release the resourece, but
it''s still locked. anybody know how to get around this?

tks

推荐答案

Dica,


FileInfo实例上的Create方法返回一个FileStream,

有一个打开文件的句柄。


你应该做的是请改用FileStream类。如果文件

存在,则创建它,否则,打开文件。


看来你想要追加到文件的末尾(不是

覆盖它)。在这种情况下,我会使用Create的返回值,如果

文件不存在。如果是,那么你想调用FileStream

构造函数,从FileMode枚举中传入Append值。


然后,使用FileStream实例,你可以把它传递给你的

XmlTextWriter。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


Dica < GE ***** @ hotmail.com>在留言中写道

新闻:_L ****************************** @ rogers.com .. 。
Dica,

The Create method on the FileInfo instance returns a FileStream which
has a handle to the opened file.

What you should do is use the FileStream class instead. If the file
exists, then create it, otherwise, open the file.

It appears that you want to append to the end of the file as well (not
overwrite it). In that case, I would use the return value from Create if
the file doesn''t exist. If it does, then you want to call the FileStream
constructor, passing in the Append value from the FileMode enumeration.

Then, with the FileStream instance, you can pass that to your
XmlTextWriter.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dica" <ge*****@hotmail.com> wrote in message
news:_L******************************@rogers.com.. .
我得到一个错误,这个过程无法访问该文件,因为它被另一个进程使用了​​。我写完之后创建了一个新文件并尝试
打开它以便用xmlTextWriter编写。
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

如果(!bFileExists)

{/>
oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME,null); //在这里疯狂

我已经尝试将oFileInfo设置为null,希望释放resourece,但
它仍然被锁定。有人知道怎么解决这个问题吗?

i''m getting an error about ''the process cannot access the file becaise it
is
being used by another process.'' write after i''ve created a new file and
try
to open it for writing with xmlTextWriter.
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if (!bFileExists)

{

oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME, null); // craps out here

i''ve tried setting oFileInfo to null hoping to release the resourece, but
it''s still locked. anybody know how to get around this?

tks



Dica< ge ***** @ hotmail.com>写道:
Dica <ge*****@hotmail.com> wrote:
我得到一个错误''流程无法访问该文件,因为它正被另一个进程使用。''写完后我创建了一个新的文件并尝试使用xmlTextWriter打开它。
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if(!bFileExists)

oFileInfo.Create();

}
oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME,null); //在这里疯狂

我已经尝试将oFileInfo设置为null,希望释放resourece,但
它仍然被锁定。有人知道怎么解决这个问题吗?
i''m getting an error about ''the process cannot access the file becaise it is
being used by another process.'' write after i''ve created a new file and try
to open it for writing with xmlTextWriter.
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if (!bFileExists)

{

oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME, null); // craps out here

i''ve tried setting oFileInfo to null hoping to release the resourece, but
it''s still locked. anybody know how to get around this?




将变量设置为null除了停止变量

之外什么也不做垃圾收集器下次运行时收集垃圾的对象。


完成后,你应该在XmlTextWriter上调用Close,

确保你在finally块中执行它,所以即使抛出

异常也会调用它。真的很遗憾它没有实现

IDisposable,让你使用使用声明...


-

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

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



Setting a variable to null won''t do anything except stop that variable
from preventing the object from being garbage collected next time the
garbage collector runs.

You should call Close on the XmlTextWriter when you''re done with it,
making sure you do it in a finally block so it''s called even if an
exception is thrown. It''s a real shame it doesn''t implement
IDisposable, to let you use a "using" statement...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too




Nicholas Paldino [.NET / C#MVP]" < mv*@spam.guard.caspershouse.com>写在

消息新闻:uq ************** @ TK2MSFTNGP15.phx.gbl ...

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uq**************@TK2MSFTNGP15.phx.gbl...
Dica,
FileInfo实例上的Create方法返回一个FileStream,它具有打开文件的句柄。


啊,是的,这是有道理的。请求帮助。

您应该做的是使用FileStream类。如果文件
存在,则创建它,否则,打开文件。

看来你想要追加到文件的末尾(不是覆盖它) )。在这种情况下,如果文件不存在,我会使用Create的返回值。如果是,那么你想调用FileStream
构造函数,从FileMode枚举中传入Append值。

然后,使用FileStream实例,你可以将它传递给你的
XmlTextWriter。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP]
- mv*@spam.guard.caspershouse.com

Dica < GE ***** @ hotmail.com>在消息中写道
新闻:_L ****************************** @ rogers.com ..
Dica,

The Create method on the FileInfo instance returns a FileStream which
has a handle to the opened file.
ah, yes, that makes sense. tks for the help.

What you should do is use the FileStream class instead. If the file
exists, then create it, otherwise, open the file.

It appears that you want to append to the end of the file as well (not
overwrite it). In that case, I would use the return value from Create if
the file doesn''t exist. If it does, then you want to call the FileStream
constructor, passing in the Append value from the FileMode enumeration.

Then, with the FileStream instance, you can pass that to your
XmlTextWriter.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dica" <ge*****@hotmail.com> wrote in message
news:_L******************************@rogers.com.. .
我收到一个错误'进程无法访问该文件因为
它正被另一个进程使用。''写完后我创建了一个新文件并尝试使用xmlTextWriter打开它。
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if(!bFileExists)

{/>
oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME,null); //在这里疯狂

我已经尝试将oFileInfo设置为null希望释放resourece,
但它仍然被锁定。有谁知道如何解决这个问题?

i''m getting an error about ''the process cannot access the file becaise it is
being used by another process.'' write after i''ve created a new file and
try
to open it for writing with xmlTextWriter.
FileInfo oFileInfo = new FileInfo(STR_FILE_NAME);

bool bFileExists = oFileInfo.Exists;

if (!bFileExists)

{

oFileInfo.Create();

}

oFileInfo = null;

bankWriter = new XmlTextWriter(STR_FILE_NAME, null); // craps out here

i''ve tried setting oFileInfo to null hoping to release the resourece, but it''s still locked. anybody know how to get around this?

tks




这篇关于无法写入新创建的文件 - 另一个进程正在使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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