Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenFileDialog() locks the folder

Tags:

c#

silverlight

I use OpenFileDialog() in my Silverlight application. When I select a file using ShowDialog() it simply locks the file until I close my application.

I am not able to rename or delete the folder when the application is running (silverlight application in browser)

If I try to select any other file in any another folder, I am able to rename the previous folder. It seems it is releasing the handle.

My goal: I want to rename/delete the folder in filesystem (manually) once I finished uploading.

I know it is not possible to point OpenFileDialog() to some other folder from code. Any pointers?

Btw, this is the windows error message:

The action can't be completed because the folder is open in another program. Close the folder and try again.

like image 975
DigitalManic Avatar asked Jan 22 '10 08:01

DigitalManic


2 Answers

I had the same problem. The follow fixed it

Stream fileStream = openFileDialog1.OpenFile();
if (fileStream != null)
{
   ...
   fileStream.Close();
}

By closing the stream my problem went away... :P

like image 61
James van der Walt Avatar answered Oct 07 '22 01:10

James van der Walt


using(var fileStream = openFileDialog1.OpenFile())
{
   // do your stuff

}

this will close the stream and fix your problem.

like image 22
Rohit Avatar answered Oct 07 '22 02:10

Rohit