SaveFileDialog锁定文件? [英] SaveFileDialog locks the file?

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

问题描述

我使用Google搜索,在网络和新闻组中搜索,并且在此主题上找不到任何内容。希望这意味着我只是不明白我应该在这里做什么。 :)


问题:


我正在尝试使用SaveFileDialog类来获取文件名,这是

随后打开写入(写访问,读共享,但使用

读/写共享并不会使问题消失)。有时,在

上我实际打开文件的语句(使用来自

SaveFileDialog实例的FileName字符串)我得到一个IOException抱怨该文件

正被另一个进程使用(说它不能打开......因为它是另一个进程使用的
)。


因为这个错误发生的次数越多,我点击通过

用户界面就越快,它强烈建议某处有一些未收集的资源

导致问题。但是,我无法使用我所知道的

机制来解决这些问题(使用或调用

GC.Collect(),例如)。


我已将问题缩小到SaveFileDialog本身。也就是说,如果

我将文件名硬编码为字符串并反复运行save

命令,我永远不会得到错误,无论我如何快速重新保存提交

及以上。但是,如果我包含SaveFileDialog,那么错误通常会非常有用,有时甚至是在给定的

程序执行期间第一次访问文件时。


不是我认为这种事情应该是必要的,但我已经尝试了两种不同的可能的解决办法,这两种办法都没有解决问题:


string szFile = null;


使用(SaveFileDialog sfd = new SaveFileDialog())

{

if(sfd.ShowDialog(this)== DialogResult.OK)

{

szFile = sfd.FileName;

}

}


if(szFile!= null)

{

//打开文件这里......这是发生异常的地方

}


而且,或者:


SaveFileDialog sfd =新的SaveFileDialog();


if(sfd.ShowDialog(this)== DialogResult.OK)

{

string szFile = sfd.FileName;


sfd = null;

GC。收集();


//在这里打开文件...这是异常发生的地方

}

$ b $那么,有什么想法吗?谁看过这个吗?正如我所提到的,如果我不使用

SaveFileDialog而只是直接在

代码中设置文件名变量,问题就完全消失了。对我来说似乎很明显,

SaveFileDialog以某种方式保持文件打开,但我不明白为什么,

我也不明白我怎么能得到它发布文件,以便我自己打开

.


非常感谢,

Pete

解决方案



" Peter Duniho" < Np ********* @NnOwSlPiAnMk.com在留言中写道

新闻:12 ************ @ corp.supernews.com ...


>我使用Google,网络和新闻组进行搜索,发现此主题没有任何内容。希望这意味着我只是不明白我应该在这里做什么。 :)


问题:


我正在尝试使用SaveFileDialog类来获取文件名,这是

随后打开写入(写访问,读共享,但使用

读/写共享并不会使问题消失)。有时,

在我实际打开文件的语句中(使用FileName字符串
来自SaveFileDialog实例的
)我得到一个IOException抱怨

该文件正在被另一个进程使用(说它不能打开......因为它是另一个进程正在使用的
)。



尝试使用SaveFileDialog.OpenFile而不是FileName属性?


" Ben Voigt" < rb*@nospam.nospamwrote in message

news:e4 ************** @ TK2MSFTNGP02.phx.gbl ...


尝试使用SaveFileDialog.OpenFile而不是FileName属性?



我可能会看看它是否也有问题,所以我可以提供更完整的错误报告。但OpenFile方法并没有提供

任何用于指定文件访问,缓冲区大小等的参数,因此

在我的特定情况下是不合适的(即默认情况下)开放行为不是足够的b $ b。即使问题消失了,也不是这个特殊案例的解决方案。 :(


谢谢,

Pete


Hi Pete,

对不起,我没有解决方案,但我会确认我已经看到了这个

行为。我没有将它与SF对话框相关联;它我总是认为这不是偶然和不一致的。我只是认为这是VS8的另一个很多

小错误...... :(

...那么SP1在哪里?


我会尝试点击更快然后看看会发生什么。


Emby


Peter Duniho< Np ********* @ NnOwSlPiAnMk.com写信息

news:12 *** ********* @ corp.supernews.com ...


>我使用Google,网络和新闻组搜索,发现
没有关于这个主题。希望这意味着我只是不明白什么
我应该在这里做。:)


问题:


我正在尝试使用SaveFileDialog类获取一个文件名,后来打开了写入文件(写入访问,读取共享,但是使用

读/写共享并没有使用问题就是消失了)。有时,

在我实际打开文件的语句中(使用FileName字符串
来自SaveFileDialog实例的
)我得到一个IOException抱怨

该文件正在被另一个进程使用(说它不能打开......因为它是另一个进程正在使用的
)。


因为这个错误更频繁地发生,我点击通过

用户界面时,它强烈建议有一些未收集的资源

导致问题。但是,我无法使用我所知道的解决这些问题的机制修复

问题

(使用或调用GC.Collect(),for例如)。


我已将问题缩小到SaveFileDialog本身。也就是说,如果我把文件名硬编码为一个字符串并反复运行

save命令,我就永远不会得到错误,无论我多么快地重新保存

文件一遍又一遍。但是,如果我包含SaveFileDialog,则错误

经常发生,有时甚至在给定程序执行期间首次访问文件

时。


并不是说我认为这种事情应该是必要的,但我已经尝试了两种不同的解决办法来解决这个问题: $ b br />

string szFile = null;


使用(SaveFileDialog sfd = new SaveFileDialog())

{

if(sfd.ShowDialog(this)== DialogResult.OK)

{

szFile = sfd.FileName;

}

}


if(szFile!= null)

{

//打开文件这里......这是发生异常的地方

}


而且,或者:


SaveFileDialog sfd =新的SaveFileDialog();


if(sfd.ShowDialog(this)== DialogResult.OK)

{

strin g szFile = sfd.FileName;


sfd = null;

GC.Collect();


/ /在这里打开文件......这是异常发生的地方

}


那么,有什么想法吗?谁看过这个吗?正如我所提到的,如果我不是
使用SaveFileDialog而只是在代码中直接设置文件名变量

,问题就完全消失了。对我来说很明显,

SaveFileDialog以某种方式保持文件打开,但我不知道
理解为什么,我也不明白我怎么能得到它发布文件

以便我自己打开它。


非常感谢,

Pete


I searched using Google, on the web and in the newsgroups, and found nothing
on this topic. Hopefully that means I just don''t understand what I''m
supposed to be doing here. :)

The problem:

I am trying to use the SaveFileDialog class to get a filename, which is
subsequently opened for writing (write access, read sharing, but using
read/write sharing doesn''t make the problem go away anyway). Sometimes, on
the statement where I actually open the file (using the FileName string from
the SaveFileDialog instance) I get an IOException complaining that the file
is in use by another process (says it can''t open "...because it is being
used by another process").

Because this error occurs more often the more quickly I click through the
UI, it suggested strongly that there was some uncollected resource somewhere
causing problems. However, I have been unable to fix the problem using the
mechanisms I know of for addressing those issues ("using" or calling
GC.Collect(), for example).

I have narrowed the problem down to the SaveFileDialog itself. That is, if
I instead hard-code the filename as a string and repeatedly run the save
command, I never get the error, no matter how quickly I resave the file over
and over. If, however, I include the SaveFileDialog, the error occurs quite
often, sometimes even on the very first access of the file during a given
program execution.

Not that I think this sort of thing should be necessary, but I''ve tried two
different possible work-arounds, neither of which fixed the problem:

string szFile = null;

using (SaveFileDialog sfd = new SaveFileDialog())
{
if (sfd.ShowDialog(this) == DialogResult.OK)
{
szFile = sfd.FileName;
}
}

if (szFile != null)
{
// open file here...this is where the exception occurs
}

And, alternatively:

SaveFileDialog sfd = new SaveFileDialog();

if (sfd.ShowDialog(this) == DialogResult.OK)
{
string szFile = sfd.FileName;

sfd = null;
GC.Collect();

// open file here...this is where the exception occurs
}

So, any ideas? Has anyone seen this before? As I mentioned, if I don''t use
the SaveFileDialog and instead just set the filename variable directly in
code, the problem completely disappears. It seems obvious to me that the
SaveFileDialog is somehow keeping the file open, but I don''t understand why,
nor do I understand how I can get it to release the file so that I can open
it myself.

Much thanks,
Pete

解决方案


"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:12************@corp.supernews.com...

>I searched using Google, on the web and in the newsgroups, and found
nothing on this topic. Hopefully that means I just don''t understand what
I''m supposed to be doing here. :)

The problem:

I am trying to use the SaveFileDialog class to get a filename, which is
subsequently opened for writing (write access, read sharing, but using
read/write sharing doesn''t make the problem go away anyway). Sometimes,
on the statement where I actually open the file (using the FileName string
from the SaveFileDialog instance) I get an IOException complaining that
the file is in use by another process (says it can''t open "...because it
is being used by another process").

Tried using SaveFileDialog.OpenFile instead of the FileName property?


"Ben Voigt" <rb*@nospam.nospamwrote in message
news:e4**************@TK2MSFTNGP02.phx.gbl...

Tried using SaveFileDialog.OpenFile instead of the FileName property?

I may look at that just to see whether it also has the problem, so I can
provide a more complete bug report. But the OpenFile method doesn''t provide
any parameters for specifying file access, buffer size, etc. and so is
unsuitable in my particular situation (ie, the default open behavior isn''t
sufficient). Even if it makes the problem go away, it''s not a solution for
this particular case. :(

Thanks,
Pete


Hi Pete,

Sorry, I don''t have a solution, but I will confirm that I''ve seen this
behavior. I''ve not correlated it to the SF dialog; it has always seemed
infrequent and inconsistent. I just figured it was yet another of VS8''s many
little bugs ... :(
... so where is that SP1??

I''ll try "clicking faster" and see what happens.

Emby

"Peter Duniho" <Np*********@NnOwSlPiAnMk.comwrote in message
news:12************@corp.supernews.com...

>I searched using Google, on the web and in the newsgroups, and found
nothing on this topic. Hopefully that means I just don''t understand what
I''m supposed to be doing here. :)

The problem:

I am trying to use the SaveFileDialog class to get a filename, which is
subsequently opened for writing (write access, read sharing, but using
read/write sharing doesn''t make the problem go away anyway). Sometimes,
on the statement where I actually open the file (using the FileName string
from the SaveFileDialog instance) I get an IOException complaining that
the file is in use by another process (says it can''t open "...because it
is being used by another process").

Because this error occurs more often the more quickly I click through the
UI, it suggested strongly that there was some uncollected resource
somewhere causing problems. However, I have been unable to fix the
problem using the mechanisms I know of for addressing those issues
("using" or calling GC.Collect(), for example).

I have narrowed the problem down to the SaveFileDialog itself. That is,
if I instead hard-code the filename as a string and repeatedly run the
save command, I never get the error, no matter how quickly I resave the
file over and over. If, however, I include the SaveFileDialog, the error
occurs quite often, sometimes even on the very first access of the file
during a given program execution.

Not that I think this sort of thing should be necessary, but I''ve tried
two different possible work-arounds, neither of which fixed the problem:

string szFile = null;

using (SaveFileDialog sfd = new SaveFileDialog())
{
if (sfd.ShowDialog(this) == DialogResult.OK)
{
szFile = sfd.FileName;
}
}

if (szFile != null)
{
// open file here...this is where the exception occurs
}

And, alternatively:

SaveFileDialog sfd = new SaveFileDialog();

if (sfd.ShowDialog(this) == DialogResult.OK)
{
string szFile = sfd.FileName;

sfd = null;
GC.Collect();

// open file here...this is where the exception occurs
}

So, any ideas? Has anyone seen this before? As I mentioned, if I don''t
use the SaveFileDialog and instead just set the filename variable directly
in code, the problem completely disappears. It seems obvious to me that
the SaveFileDialog is somehow keeping the file open, but I don''t
understand why, nor do I understand how I can get it to release the file
so that I can open it myself.

Much thanks,
Pete



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

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