0

In a WinForms project I am trying to extract archive in destination folder, but I not want to extract the folder from archive in destination folder.

I want to extract only the file.

My code:

private void button1_Click(object sender, EventArgs e)
        {

            string Info = "";


            string extractPath = @"D:\dosfiles\SYNOPD\SYNOPD$$.PD\" + comboBox3.SelectedItem.ToString() + @"\" + comboBox2.SelectedItem.ToString() + @"\";
            string zipPath = @"D:\dosfiles\" + comboBox1.SelectedItem.ToString() + "_" + comboBox2.SelectedItem.ToString() + "_" + comboBox3.SelectedItem.ToString() + "S" + ".zip";

            if (!Directory.Exists(extractPath))
            {
                Info += "Folder not exists in D:\\dosfiles\\SYNOPD\\SYNOPD$$.PD\\" + comboBox3.SelectedItem.ToString() + @"\" + comboBox2.SelectedItem.ToString() + @"\" + comboBox1.SelectedItem.ToString();
            }

            else if(Directory.Exists(extractPath))
            {
                if (Directory.Exists(extractPath))
                {
                    Directory.Delete(extractPath, true);
                }
                ZipFile.ExtractToDirectory(zipPath, extractPath);
                Info += "Your synoptic files has been extracted in  D:\\dosfiles\\SYNOPD\\SYNOPD$$.PD\\" + comboBox3.SelectedItem.ToString() + @"\" + comboBox2.SelectedItem.ToString() + @"\" + comboBox1.SelectedItem.ToString() + Environment.NewLine;
            }

            if (Info != "")
            {
                MessageBox.Show(Info);
                Application.Exit();
            }
        }
    }
}

I want to remove this folder.

Richard
  • 106,783
  • 21
  • 203
  • 265
Pizhev Racing
  • 466
  • 1
  • 6
  • 23
  • 1
    *First*: `if (!Directory.Exists(extractPath))` doesn't require an `else if(Directory.Exists(extractPath))` because that is already implied. *Second*: `else if(Directory.Exists(extractPath))` doesn't require an `if (Directory.Exists(extractPath))` because you just checked for that to be true. *Third*: You have to extract everything (into a [new temp folder](https://stackoverflow.com/a/278457/12258072)), copy over the files you need and then delete the temp folder. Or use a library that supports extracting only specific files, I am not aware that `System.IO.Compression.ZipFile` supports that. – Longoon12000 Jan 07 '20 at 15:17
  • I agree with the idea proposed by Longoon12000. Maybe you can use the follow code to determine if the object is a file. `void FileOrFolder(string path){ if (File.Exists(path)) { Console.WriteLine("File"); } else if (Directory.Exists(path)) { Console.WriteLine("Folder"); } }`. – 大陸北方網友 Jan 08 '20 at 02:02
  • Can I get example ? – Pizhev Racing Jan 08 '20 at 07:55

0 Answers0