通过循环数组来归档每个字符串
我目前正在制作一个软件,它允许用户输入最多 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。
我希望我给了大家足够的信息。
I am currently making a piece of software that will allow the user to enter up to 6 directories, each directory is saved as a string (within an array) the loop is then meant to check through the array and any that are not null i.e. actually have a directory assigned are meant to be zipped into their own archive. This is the code I have so far.
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);
}
}
}
}
What I am trying to get this to do is save the first user given location to tmpZip0.7z, the second to tmpZip1.7z and so on however at the moment all it is doing is adding each directory to tmpZip0.zip.
Also as a side note, how would I get it to name each archive after the directory selected to be archived?
I am currently using DotNetZip (Ionic.Zip) dll.
I hope I gave enough information guys.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要切换一些内容:
原因:
Destination
在创建后就被固定了。它不会改变,只是因为您增加了nxtFileNum
。ZipFile
并且仅递增了nxtFileNum
一次,因为它们位于foreach
循环之外if
确保只有在真正使用实例时才会创建实例。You need to switch some stuff:
Reasons:
Destination
is fixed after you created it. It doesn't change, just because you incrementnxtFileNum
.ZipFile
and you incrementednxtFileNum
only once, because the those were outside of yourforeach
loopif
makes sure an instance is only created if it is really used.好吧,您可以这样做:
不过,这显然会拉动所有文本框。另一种方法是使用
List
类型的集合或类似的集合来代替六个不同的变量。请注意,即使用户没有指定前三个名称,始终会创建 .1、.2、.3 等。如果您想绝对忠实于用户给出的定位,请告诉我。
顺便说一句,我不清楚您是否真的应该重用相同的 ZipFile 对象。我希望这更合适:(
顺便说一下,请注意我如何将变量重命名为更常规 - 变量通常以小写字母开头。)
Well, you can do this with:
That will obviously pull all the textboxes though. An alternative is to have a collection of type
List<TextBox>
or something similar instead of the six different variables.Note that that will always create .1, .2, .3 etc even if the user didn't specify the first three names. Let me know if you want to be absolutely faithful to the positioning the user gave.
It's not clear to me that you should really be reusing the same
ZipFile
object, by the way. I'd expect this to be more appropriate:(Note how I've renamed the variables to be more conventional, by the way - variables typically start with a lower case letter.)