none
Zip a folder and password protect in c# RRS feed

  • Question

  • Can anyone help me in how to zip a folder and password protect it.  I've attached my code for what I've done so far in creating the folder etc.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    
    namespace SFHS
    {
      public partial class Form1 : Form
      {
        string mainPath = @"c:\SFHS\";
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
    
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          string sourcePath = mainPath + "template";
    
          //Create a new subfolder under the current active folder
          string newPath = Path.Combine(mainPath, txtRelease.Text);
    
          // To copy a folder's contents to a new location:
          // Create a new target folder, if necessary.
          if (!Directory.Exists(newPath))
          {
            Directory.CreateDirectory(newPath);
            
          }
          else
          {
            MessageBox.Show("Source path already exists");
    
          }
    
    
          if (Directory.Exists(sourcePath))
          {
            string[] files = Directory.GetFiles(sourcePath);
            string filename = "";
            string destFilename = "";
    
    
            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
              // Use static Path methods to extract only the file name from the path.
              filename = Path.GetFileName(s);
              destFilename = Path.Combine(newPath, filename.Replace("template", txtRelease.Text));
              File.Copy(s, destFilename, true);
    
            }
          }
    
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
          
          string text = "A class is the most powerful data type in C#. Like structures, " +
                  "a class defines the data and behavior of the data type. ";
          File.WriteAllText(mainPath + txtRelease.Text + @"\data_" + txtRelease.Text + ".txt", text);
    
        }
    		    
    	}
    
    
    }
    
    
    
    
    Thursday, January 20, 2011 4:52 PM

Answers