Click here to Skip to main content
14,579,459 members
Rate this:
Please Sign up or sign in to vote.
Hi i have windows form application where i generate an XML file and what i looking for is to save automatically the XML inside zip file

You find below export button code:

What I have tried:

SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
            saveFileDialog1.Filter = "Extension|*.xml";
            saveFileDialog1.FilterIndex = 1;
            saveFileDialog1.FileName = String.Format("{0}", txtBox.Text + "_" + comBo1.Text + "_" + comBo2.Text);
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {

                doc.Save(saveFileDialog1.FileName);
            }
Posted
Updated 21-May-20 17:28pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Add a reference & 'using' statement for System.IO.Compression.FileSystem

ref : ZipArchive Constructor (System.IO.Compression) | Microsoft Docs[^] - using the 'stream constructor' gets-around having to add an entire directory

using (FileStream fs = new FileStream(@"drive:\path\file.zip",FileMode.Create))
using (ZipArchive zArchive = new ZipArchive(fs, ZipArchiveMode.Create))
{
    zArchive.CreateEntryFromFile(@"drive:\path\xml-file.xml", "xml-file.xml");
}
   
Comments
Maciej Los 22-May-20 2:15am
   
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100