ZipFile 压缩未选择组合框值

Combobox value is not selected by ZipFile compression

我正在创建 Windows Application.I 我正在使用 FolderBrowserDialog textBox1 ComboBox 和按钮。在我的按钮中单击我想 select 一个组合框值并存储在一个 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");
            }        
        }
    }
}

根据您上面的评论:

System.IO.FileNotFoundException

您正在使用的

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

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

你的当前工作目录中真的有一个名为 "s" 的文件吗?运行时告诉你,你不知道。我倾向于相信它。您不能添加不存在的文件。

有一个 字符串变量 名为 s:

string s = (string)comboBox.SelectedItem;

也许你打算用那个?:

zip.AddFile(s, "files");