通过循环遍历数组来归档每个字符串



我目前正在制作一个软件,允许用户输入多达6个目录,每个目录保存为字符串(在数组内),然后循环意味着通过数组和任何不为空的检查,即实际上有一个分配的目录意味着被压缩到他们自己的存档。这是我目前得到的代码。

        private void ZipIt()
        {
        int nxtFileNum = 0;
        string Destination = @"C:tmpZip" + nxtFileNum + ".zip";
        // Check all fields, check if empty, if not save to Selection array
        // Seems a inefficient - Possibly loop through Text box control type and collect?
        if (String.IsNullOrEmpty(tboxSelect1.Text) == false) { BckupArray[0] = tboxSelect1.Text; };
        if (String.IsNullOrEmpty(tboxSelect2.Text) == false) { BckupArray[1] = tboxSelect2.Text; };
        if (String.IsNullOrEmpty(tboxSelect3.Text) == false) { BckupArray[2] = tboxSelect3.Text; };
        if (String.IsNullOrEmpty(tboxSelect4.Text) == false) { BckupArray[3] = tboxSelect4.Text; };
        if (String.IsNullOrEmpty(tboxSelect5.Text) == false) { BckupArray[4] = tboxSelect5.Text; };
        if (String.IsNullOrEmpty(tboxSelect6.Text) == false) { BckupArray[5] = tboxSelect6.Text; };
        // Create a new ZipFile entity and then loop through each array member, checking if
        // it has an assigned value, if so compress it, if not, skip it.
        using (ZipFile ZipIt = new ZipFile())
        {
            nxtFileNum++;
            foreach (String q in BckupArray)
            {
                if (q != null)
                {
                    ZipIt.AddDirectory(q);
                    ZipIt.Comment = "This archive was created at " + System.DateTime.Now.ToString("G");
                    ZipIt.Save(Destination);
                }
            }
        }        
    }

我要做的是将第一个用户给定的位置保存到tmpZip0.7z,第二个保存到tmpZip1.7z等等,但是目前它所做的就是将每个目录添加到tmpZip0.zip。


还有一个旁注,我如何让它以要存档的目录命名每个存档?

我目前正在使用DotNetZip (Ionic.Zip) dll.

我希望我给了足够的信息。

你需要切换一些东西:

foreach (String q in BckupArray)
{
    nxtFileNum++;
    if (q != null)
    {
        using (ZipFile ZipIt = new ZipFile())
        {
            string Destination = @"C:tmpZip" + nxtFileNum + ".zip";
            ZipIt.AddDirectory(q);
            ZipIt.Comment = "This archive was created at " + 
                            System.DateTime.Now.ToString("G");
            ZipIt.Save(Destination);
        }
    }
}     

原因:

  1. 字符串Destination在您创建它之后是固定的。它不会改变,只是因为你增加了nxtFileNum
  2. 你只创建了一个ZipFile,你只增加了nxtFileNum一次,因为这些都在你的foreach循环之外
  3. 将创建ZipFile的部分放入if中,确保只有在真正使用实例时才创建实例。

嗯,你可以这样做:

var strings = Controls.OfType<TextBox>()
                      .Select(x => x.Text)
                      .Where(text => !string.IsNullOrEmpty(text))
                      .ToList();
using (ZipFile ZipIt = new ZipFile())
{
    nxtFileNum++;
    string comment = string.Format("This archive was created at {0:G}",
                                   DateTime.Now);
    foreach (string directory in strings)
    {
        ZipIt.AddDirectory(directory);
        ZipIt.Comment = comment;
        ZipIt.Save(Destination + "." + nxtFileNum);
    }
}   

这显然会拉出所有的文本框。另一种选择是使用List<TextBox>或类似类型的集合,而不是六个不同的变量。

请注意,即使用户没有指定前三个名称,也将始终创建.1,.2,.3等。如果你想绝对忠实于用户给出的定位,请告诉我。

顺便说一下,我不清楚你是否真的应该重用相同的ZipFile对象。我希望这样更合适:
string comment = string.Format("This archive was created at {0:G}",
                               DateTime.Now);
int fileIndex = 0;
foreach (string directory in strings)
{
    fileIndex++;
    using (ZipFile zipFile = new ZipFile())
    {
        zipFile.AddDirectory(directory);
        zipFile.Comment = comment;
        zipFile.Save(Destination + "." + fileIndex);
    }
}

(顺便说一下,请注意我是如何将变量重命名为更常规的—变量通常以小写字母开头。)

最新更新