file access problem

  • Thread starter Thread starter Saso Zagoranski
  • Start date Start date
S

Saso Zagoranski

Hi!

Here is what I'm trying to do:

I have created a UserControl named PictureView. It holds all the images
in one directory in the Bitmap[] images variable.
The selected image is displayed in the PictureBox control.

Here is how it works.

1. I get all the filenames of the images in the specified directory
2. I open the Bitmaps (I used Bitmap.FromFile but found out that it locks
the files so I
open them using FileStream).

The user can not click "left" or "right" buttons and view the images.

The images are "bound" to an item. So if the code of an item is "1" it's
images are stored
in : (baseDir)\images\1...

The problem comes when the user presses the "delete" button. Here is what I
do then:

1. I close the file streams I dispose of ALL the images
2. I put each of the images = null;
3. I put the images[] = null;
4. I dispose of the image in the PictureBox

And I call the Garbage collector just in case...

Then I try to delete the file. Here are the errors that I get:
"Cannot delete file (correct path) because It's being used by another
process"
or
"Cannot ......... Access denied"

If I go and look at the properties of the directory I see that it is set to
"read-only" and it seems
that windows (or .net fw) is manually changing this to read-only every time
I try to add another picture
or everytime I close and re-open the application.

This is really driving me crazy :(

Help? Anyone?
 
Hi!

Here is what I'm trying to do:

I have created a UserControl named PictureView. It holds all the images
in one directory in the Bitmap[] images variable.
The selected image is displayed in the PictureBox control.

Here is how it works.

1. I get all the filenames of the images in the specified directory
2. I open the Bitmaps (I used Bitmap.FromFile but found out that it locks
the files so I
open them using FileStream).

The user can not click "left" or "right" buttons and view the images.

The images are "bound" to an item. So if the code of an item is "1" it's
images are stored
in : (baseDir)\images\1...

The problem comes when the user presses the "delete" button. Here is what I
do then:

1. I close the file streams I dispose of ALL the images
2. I put each of the images = null;
3. I put the images[] = null;
4. I dispose of the image in the PictureBox

And I call the Garbage collector just in case...

Then I try to delete the file. Here are the errors that I get:
"Cannot delete file (correct path) because It's being used by another
process"
or
"Cannot ......... Access denied"

If I go and look at the properties of the directory I see that it is set to
"read-only" and it seems
that windows (or .net fw) is manually changing this to read-only every time
I try to add another picture
or everytime I close and re-open the application.

This is really driving me crazy :(

Help? Anyone?

Hi Saso,

Consider this code:

pictureBox1.Image = Bitmap.FromFile (sFileName);

// now try to delete it
pictureBox1.Image = null;
File.Delete (sFileName);

This returns an error (file used by another process).

But if you use pictureBox1.Image.Dispose(); you won't get an error. So I
think instead of setting images[] = null try disposing them.

Hope it would help you in some way :)

lp,
Peter
 
If you look at the "process" I've described you can see that I do dispose
the images before setting them to null.

This is the entire code:

for ( int i = 0;i < images.Length;i++ )
{
this.fileStreams.Close();
this.images.Dispose();
this.images = null;
}

images = null;
fileStream = null;

this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;

System.GC.Collect();

Now I try to delete the file and I get the mentioned exceptions... I think
it has something to do
with Windows setting the directory "read-only"... The problem is I don't
know why or when this happens...

Peter Jausovec said:
Hi!

Here is what I'm trying to do:

I have created a UserControl named PictureView. It holds all the images
in one directory in the Bitmap[] images variable.
The selected image is displayed in the PictureBox control.

Here is how it works.

1. I get all the filenames of the images in the specified directory
2. I open the Bitmaps (I used Bitmap.FromFile but found out that it locks
the files so I
open them using FileStream).

The user can not click "left" or "right" buttons and view the images.

The images are "bound" to an item. So if the code of an item is "1" it's
images are stored
in : (baseDir)\images\1...

The problem comes when the user presses the "delete" button. Here is what I
do then:

1. I close the file streams I dispose of ALL the images
2. I put each of the images = null;
3. I put the images[] = null;
4. I dispose of the image in the PictureBox

And I call the Garbage collector just in case...

Then I try to delete the file. Here are the errors that I get:
"Cannot delete file (correct path) because It's being used by another
process"
or
"Cannot ......... Access denied"

If I go and look at the properties of the directory I see that it is set to
"read-only" and it seems
that windows (or .net fw) is manually changing this to read-only every time
I try to add another picture
or everytime I close and re-open the application.

This is really driving me crazy :(

Help? Anyone?

Hi Saso,

Consider this code:

pictureBox1.Image = Bitmap.FromFile (sFileName);

// now try to delete it
pictureBox1.Image = null;
File.Delete (sFileName);

This returns an error (file used by another process).

But if you use pictureBox1.Image.Dispose(); you won't get an error. So I
think instead of setting images[] = null try disposing them.

Hope it would help you in some way :)

lp,
Peter
 
If you look at the "process" I've described you can see that I do dispose
the images before setting them to null.

This is the entire code:

for ( int i = 0;i < images.Length;i++ )
{
this.fileStreams.Close();
this.images.Dispose();
this.images = null;
}

images = null;
fileStream = null;

this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;

System.GC.Collect();

Now I try to delete the file and I get the mentioned exceptions... I think
it has something to do
with Windows setting the directory "read-only"... The problem is I don't
know why or when this happens...

Peter Jausovec said:
Hi!

Here is what I'm trying to do:

I have created a UserControl named PictureView. It holds all the images
in one directory in the Bitmap[] images variable.
The selected image is displayed in the PictureBox control.

Here is how it works.

1. I get all the filenames of the images in the specified directory
2. I open the Bitmaps (I used Bitmap.FromFile but found out that it locks
the files so I
open them using FileStream).

The user can not click "left" or "right" buttons and view the images.

The images are "bound" to an item. So if the code of an item is "1" it's
images are stored
in : (baseDir)\images\1...

The problem comes when the user presses the "delete" button. Here is what I
do then:

1. I close the file streams I dispose of ALL the images
2. I put each of the images = null;
3. I put the images[] = null;
4. I dispose of the image in the PictureBox

And I call the Garbage collector just in case...

Then I try to delete the file. Here are the errors that I get:
"Cannot delete file (correct path) because It's being used by another
process"
or
"Cannot ......... Access denied"

If I go and look at the properties of the directory I see that it is set to
"read-only" and it seems
that windows (or .net fw) is manually changing this to read-only every time
I try to add another picture
or everytime I close and re-open the application.

This is really driving me crazy :(

Help? Anyone?

Hi Saso,

Consider this code:

pictureBox1.Image = Bitmap.FromFile (sFileName);

// now try to delete it
pictureBox1.Image = null;
File.Delete (sFileName);

This returns an error (file used by another process).

But if you use pictureBox1.Image.Dispose(); you won't get an error. So I
think instead of setting images[] = null try disposing them.

Hope it would help you in some way :)

lp,
Peter


Your code works fine. I've loaded some files, run your code and then tried
to delete one file and it works fine - maybe something other is wrong.
 
I know it SHOULD work... And I did mention... The problem is probably with
the "read-only" property switching of the directory with the images...

So I was hoping someone could point me out to some other possibilites as
what else could
be wrong and what should I check?


Peter Jausovec said:
If you look at the "process" I've described you can see that I do dispose
the images before setting them to null.

This is the entire code:

for ( int i = 0;i < images.Length;i++ )
{
this.fileStreams.Close();
this.images.Dispose();
this.images = null;
}

images = null;
fileStream = null;

this.pictureBox1.Image.Dispose();
this.pictureBox1.Image = null;

System.GC.Collect();

Now I try to delete the file and I get the mentioned exceptions... I think
it has something to do
with Windows setting the directory "read-only"... The problem is I don't
know why or when this happens...

Peter Jausovec said:
On Mon, 31 May 2004 17:47:26 +0200, Saso Zagoranski wrote:

Hi!

Here is what I'm trying to do:

I have created a UserControl named PictureView. It holds all the images
in one directory in the Bitmap[] images variable.
The selected image is displayed in the PictureBox control.

Here is how it works.

1. I get all the filenames of the images in the specified directory
2. I open the Bitmaps (I used Bitmap.FromFile but found out that it locks
the files so I
open them using FileStream).

The user can not click "left" or "right" buttons and view the images.

The images are "bound" to an item. So if the code of an item is "1" it's
images are stored
in : (baseDir)\images\1...

The problem comes when the user presses the "delete" button. Here is what I
do then:

1. I close the file streams I dispose of ALL the images
2. I put each of the images = null;
3. I put the images[] = null;
4. I dispose of the image in the PictureBox

And I call the Garbage collector just in case...

Then I try to delete the file. Here are the errors that I get:
"Cannot delete file (correct path) because It's being used by another
process"
or
"Cannot ......... Access denied"

If I go and look at the properties of the directory I see that it is
set
to
"read-only" and it seems
that windows (or .net fw) is manually changing this to read-only every time
I try to add another picture
or everytime I close and re-open the application.

This is really driving me crazy :(

Help? Anyone?

Hi Saso,

Consider this code:

pictureBox1.Image = Bitmap.FromFile (sFileName);

// now try to delete it
pictureBox1.Image = null;
File.Delete (sFileName);

This returns an error (file used by another process).

But if you use pictureBox1.Image.Dispose(); you won't get an error. So I
think instead of setting images[] = null try disposing them.

Hope it would help you in some way :)

lp,
Peter


Your code works fine. I've loaded some files, run your code and then tried
to delete one file and it works fine - maybe something other is wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top