OpenFileDialog () locks the folder

I am using OpenFileDialog() in a Silverlight application. When I select a file using ShowDialog() , it just locks the file until I close the application.

I can not rename or delete the folder when the application starts (silverlight application in the browser)

If I try to select any other file in any other folder, I can rename the previous folder. He seems to be loosing the handle.

My goal: I want to rename / delete a folder in the file system (manually) after the download is complete.

I know that it is not possible to point OpenFileDialog() to another folder from the code. Any pointers?

Btw, this is a Windows error message:

The action cannot be performed because the folder is open in another program. Close the folder and try again.

+4
source share
5 answers

I had the same problem. Next fixed is

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

Closing the stream, my problem disappeared ...: P

+2
source
 using(var fileStream = openFileDialog1.OpenFile()) { // do your stuff } 

this will close the thread and fix your problem.

+1
source

AFAIK is how the Windows file system works - you cannot rename a folder with several open files in it. Make sure you free all resources from the folder (File.Close (), etc.) before attempting to rename it.

0
source

By assigning null to all variables containing a link to OpenFileDialog and any FileInfo links or to code where all this stuff is beyond the scope, try the following: -

 GC.Collect(); 

This is a very draconian thing, and I would not recommend it usually. If this does not fix the problem, get rid of it.

0
source

To avoid this behavior, set the RestoreDirectory property property to true. You can read more here: http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.restoredirectory.aspx

0
source

Source: https://habr.com/ru/post/1299087/


All Articles