2

現在、ユーザーが最大6つのディレクトリを入力できるようにするソフトウェアを作成しています。各ディレクトリは文字列として(配列内に)保存され、ループは配列とnullではないものをすべてチェックすることを目的としています。ディレクトリが割り当てられている場合は、独自のアーカイブに圧縮されることを意図しています。これは私がこれまでに持っているコードです。

        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 に保存し、2 番目のユーザーを tmpZip1.7z に保存することですが、現時点では、各ディレクトリを tmpZip0.zip に追加するだけです。


また、補足として、アーカイブするように選択されたディレクトリの後に各アーカイブに名前を付けるにはどうすればよいですか?

現在 DotNetZip (Ionic.Zip) dll を使用しています。

十分な情報を提供したことを願っています。

4

2 に答える 2

0

さて、あなたはこれを行うことができます:

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>別の方法は、6つの異なる変数の代わりに、型または類似のもののコレクションを持つことです。

ユーザーが最初の3つの名前を指定しなかった場合でも、常に.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);
    }
}

(ちなみに、変数の名前をより一般的なものに変更したことに注意してください。変数は通常、小文字で始まります。)

于 2011-09-02T16:10:43.593 に答える
0

あなたはいくつかのものを切り替える必要があります:

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. ループの外にあるため、作成したのは1つだけで、ZipFileインクリメントは1回だけです。nxtFileNumforeach
  3. ZipFileを作成する部分をに入れるとif、インスタンスが実際に使用される場合にのみ作成されるようになります。
于 2011-09-02T16:12:41.170 に答える