Error when try to delete files that existed

2 posts, 0 answers
  1. Sam
    Sam avatar
    27 posts
    Member since:
    Sep 2013

    Posted 08 Apr 2014 Link to this post

    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
  2. Angel Petrov
    Admin
    Angel Petrov avatar
    1007 posts

    Posted 14 Apr 2014 Link to this post

    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.

     
Back to Top