0

I found out a problem in my project. I'm currently using a DotNetZip library to extract zip - files. Additionally, I need to delete a zip file after extracting it's content. However, I'm unable to do it (both manually and using File.Delete(...)), cause the folder is open in my programm. So, I think this problem is caused by file extraction process The "suspicious" code part:

string localFileName = @"...path....";

            string remoteDir = "/SFTPfolder/";

            using (var sftp = new SftpClient(SFTPHost, SFTPUsername, SFTPPassword))
            {
                sftp.Connect();

                var files = sftp.ListDirectory(remoteDir);



                foreach (var x in files)
                {
                    if (!x.Name.StartsWith("."))
                    {
                        Console.WriteLine(x.Name.ToString());

                        string rfn = x.Name;

                        using (Stream st1 = File.OpenWrite(localFileName + rfn))
                        {

                            sftp.DownloadFile(remoteDir + rfn, st1);
                        }
                    }
                }

            }

            UnzipFiles();
            File.Delete(@"BLA\BLA1\BLA2\ZIPFILE.zip");

Where UnzipFiles is:

public static void UnzipFiles()
    {

        string zipFilePath = @"BLA\BLA1\BLA2\ZIPFILE.zip";
        string extractedFilepath = @"BLA\BLA1\BLA2";

        ZipFile zipfile = ZipFile.Read(zipFilePath);
        if (!Directory.Exists(extractedFilepath))
        {
            Directory.CreateDirectory(extractedFilepath);
        }

        foreach (ZipEntry e in zipfile)
        {
            e.Extract(extractedFilepath, ExtractExistingFileAction.OverwriteSilently);
        }

    }

So, my question is: How to make this file "free"? (In order to delete it)

Fedir Katushonok
  • 391
  • 5
  • 13

0 Answers0