This is a migrated thread and some comments may be shown as answers.

Error when try to delete files that existed

1 Answer 41 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Sam
Top achievements
Rank 1
Sam asked on 08 Apr 2014, 09:58 PM
Hello,

I used RadAsyncUpload control to accept a upload zip file. My zip file contains text delimiter files.  I am able to unzip it to a temporary directory, but if I try again with the same zip file I get error something like "...because it is being used by another process." (at line 17)

Below is my method to unzip:
01.private static void UnpackAndSaveFile(ZipPackageEntry entry, string destinationPath)
02.    {
03.        Stream stream = entry.OpenInputStream();
04.        var fileName = destinationPath + "\\" + entry.FileNameInZip;
05. 
06.        var directoryName = Path.GetDirectoryName(fileName);
07. 
08.        if (directoryName != null)
09.        {
10.            if (!Directory.Exists(directoryName))
11.            {
12.                Directory.CreateDirectory(directoryName);
13.            }
14. 
15.            if (System.IO.File.Exists(fileName))
16.            {
17.                System.IO.File.Delete(fileName);
18.            }
19. 
20.            StreamReader reader = new StreamReader(stream);
21.            string text = reader.ReadToEnd();
22.            File.WriteAllText(fileName, text);          
23.        }
24.    }


Please help,

Thanks,
Sam

1 Answer, 1 is accepted

Sort by
0
Angel Petrov
Telerik team
answered on 14 Apr 2014, 06:46 AM
Hello Sam,

After examining the code I noticed that the StreamReader is not closed which is probably causing the described behavior. Please call reader.Close() before File.WriteAllText or include a using statement(as demonstrated here) and test whether this resolves the problem.

Regards,
Angel Petrov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
ZipLibrary
Asked by
Sam
Top achievements
Rank 1
Answers by
Angel Petrov
Telerik team
Share this question
or