tags:

views:

181

answers:

4

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.

A: 

AFAIK it the way windows file system work - you can't rename folder while having some open file handles inside. Make sure that you release all resources from folder(File.Close() etc.) before trying to rename it.

Bolek Tekielski
Yes, I release all resources. And I do not have any problem in renaming the files (even selected files).
DigitalManic
All resources, do you also .Dispose the filedialog ? .Close it is not enough.
nos
@nos: Ordinarily .Close and .Dispose are synonymous on Disposable objects, however in this case OpenFileDialog has neither.
AnthonyWJones
@AnthonyWJones If you are calling .ShowDialog, Close and Dispose are not synonyms. I'm assuming the OP is using System.Windows.Forms.OpenFileDialog - though being Silverlight that might not be the case.
nos
@nos: No this is a silverlight question OpenFileDialog is quite a different beast.
AnthonyWJones
SL OpenFileDialog is different. It doesn't have Dispose() or Close()
DigitalManic
+1  A: 

Having assigned null to all variables holding a reference to OpenFileDialog and any FileInfo references or in code where all that stuff is out of scope, then try this:-

GC.Collect();

Its a very draconian thing to do and I wouldn't normally recommend it. If it doesn't fix your problem get rid of it.

AnthonyWJones
It did not fix the issue.
DigitalManic
+1  A: 

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

James van der Walt
I think this is the right answer. OFD in Silverlight returns an open file stream, and you've got to close it before, well, it gets closed :-). A slightly better pattern is to declare the Stream fileStream outside of a try/catch block, and then put the "if (fileStream !=null)..." bit inside the finally{} block.
Ken Smith
A: 

To avoid such behaviour set RestoreDirectory property value to true. Here you can read more: http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.restoredirectory.aspx

Vyacheslav