P
Peter Duniho
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
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