470,628 Members | 2,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,628 developers. It's quick & easy.

file access problem

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?

Nov 16 '05 #1
4 3579
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

Nov 16 '05 #2
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[i].Close();
this.images[i].Dispose();
this.images[i] = 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" <pe************@uni-mb.si> wrote in message
news:g6***************************@40tude.net...
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

Nov 16 '05 #3
On Mon, 31 May 2004 18:37:43 +0200, Saso Zagoranski wrote:
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[i].Close();
this.images[i].Dispose();
this.images[i] = 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" <pe************@uni-mb.si> wrote in message
news:g6***************************@40tude.net...
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.
Nov 16 '05 #4
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" <pe************@uni-mb.si> wrote in message
news:n7*****************************@40tude.net...
On Mon, 31 May 2004 18:37:43 +0200, Saso Zagoranski wrote:
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[i].Close();
this.images[i].Dispose();
this.images[i] = 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" <pe************@uni-mb.si> wrote in message
news:g6***************************@40tude.net...
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.

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Lamberti Fabrizio | last post: by
3 posts views Thread by Joe Costa | last post: by
7 posts views Thread by Mark | last post: by
9 posts views Thread by JimmyKoolPantz | last post: by
4 posts views Thread by Salad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.