ZipFile压缩不选择组合框值

时间:2015-11-19 13:48:57

标签: c# zipfile

我正在创建Windows Application。我正在使用FolderBrowserDialog textBox1 ComboBox和按钮。在我的按钮单击中,我想选择一个组合框值并存储在一个zip文件中。但它不接受组合框值并显示错误。知道如何解决?

namespace WinDataStore
{
    public partial class Form1 : Form
    {
        ComboBox comboBox;

        public Form1()
        {
            InitializeComponent();
          var daysOfWeek = new[] { "RED", "GREEN", "BLUE"};

            // Initialize combo box
            comboBox = new ComboBox
            {
                DataSource = daysOfWeek,
                Location = new System.Drawing.Point(180, 140),
                Name = "comboBox",
                Size = new System.Drawing.Size(166, 21),
                DropDownStyle = ComboBoxStyle.DropDownList
            };

            // Add the combo box to the form.
            this.Controls.Add(comboBox);
        }

        private void button1_Click(object sender, EventArgs e)
        {            
            FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
            folderBrowserDlg.ShowNewFolderButton = true;         
            DialogResult dlgResult = folderBrowserDlg.ShowDialog();
            if (dlgResult.Equals(DialogResult.OK))
            {                
                textBox1.Text = folderBrowserDlg.SelectedPath;              
               Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var comboBox = this.Controls["comboBox"] as ComboBox;

            string s = (string)comboBox.SelectedItem;

            using (ZipFile zip = new ZipFile())
            {
                zip.AddFile("s", "files");

                zip.Save("z.zip");
            }        
        }
    }
}

1 个答案:

答案 0 :(得分:1)

根据您的评论:

  

System.IO.FileNotFoundException

您正在使用的

The .Add() method需要文件名:

zip.AddFile("s", "files");

你真的有一个刚刚在当前工作目录中命名为"s"的文件吗?运行时告诉你不要。而且我倾向于相信它。您无法添加不存在的文件。

有一个名为s字符串变量

string s = (string)comboBox.SelectedItem;

也许你打算用它?:

zip.AddFile(s, "files");