ZipFile压缩未选择组合框值

本文关键字:组合 选择 压缩 ZipFile | 更新日期: 2023-09-27 18:22:33

我正在创建Windows应用程序。我正在使用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");
            }        
        }
    }
}

ZipFile压缩未选择组合框值

基于您上面的评论:

System.IO.FileNotFoundException

您使用的.Add()方法需要一个文件名:

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

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

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

string s = (string)comboBox.SelectedItem;

也许你想用这个?:

zip.AddFile(s, "files");