Reputation: 530
No clue what I'm doing wrong, but I have a loop that should print 15 lines to a multiline textbox, and it's printing 10's of thousands (32,767 to be exact). I have 2 classes: my form class (DISMgui.cs) and my logic class (DISM.cs).
Form has a textbox (txtWimFile) for a file, a button (btnMount), a background worker (bwMountWim) and a ML textbox (txtOutput) for output.
Scenario: type the name of a file in txtWimFile (ex. C:\Temp\Win7x64.wim). Click btnMount. That calls bwMountWim.RunWorkerAsync() :
private void btnMount_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtWimFile.Text))
bwMountWim.RunWorkerAsync();
else
MessageBox.Show("WIM file text box returned null!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
which calls a method from the DISM class (ImageInfo):
private void bwMountWim_DoWork(object sender, DoWorkEventArgs e)
{
imageInfo = DISM.ImageInfo(GetTxtWimFile(), this);
}
ImageInfo uses the DismApi to collect info on the WIM image and returns the info :
public static DismImageInfoCollection ImageInfo(string wimFile, DISMgui that)
{
DismImageInfoCollection info = null;
try
{
info = DismApi.GetImageInfo(wimFile);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return info;
}
The the background worker RunWorkerCompleted returns the information into txtOutput.Text :
private void bwMountWim_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
foreach (DismImageInfo info in imageInfo)
{
Output = "";
Output += "Image information for image " + WimFile + System.Environment.NewLine;
Output += System.Environment.NewLine;
Output += String.Format("Image index : {0}" + System.Environment.NewLine, info.ImageIndex.ToString());
Output += String.Format("Image name : {0}" + System.Environment.NewLine, info.ImageName.ToString());
Output += String.Format("Image Internal Size : {0} MB" + System.Environment.NewLine, (info.ImageSize / 1048576).ToString("N0"));
Output += String.Format("Image Description : {0}" + System.Environment.NewLine, info.ImageDescription.ToString());
Output += String.Format("Image Type : {0}" + System.Environment.NewLine, info.ImageType.ToString());
Output += String.Format("Image Installation : {0}" + System.Environment.NewLine, info.InstallationType.ToString());
Output += String.Format("Image Prod Name : {0}" + System.Environment.NewLine, info.ProductName.ToString());
Output += String.Format("Image Prod Suite : {0}" + System.Environment.NewLine, info.ProductSuite.ToString());
Output += String.Format("Image Prod Type : {0}" + System.Environment.NewLine, info.ProductType.ToString());
Output += String.Format("Image Prod Version : {0}" + System.Environment.NewLine, info.ProductVersion.ToString());
Output += String.Format("Image Bootable : {0}" + System.Environment.NewLine, info.Bootable.ToString());
Output += String.Format("Image Architecture : {0}" + System.Environment.NewLine, info.Architecture.ToString());
Output += String.Format("Image Edition ID : {0}" + System.Environment.NewLine, info.EditionId.ToString());
}
txtOutput.Text = Output;
}
The DismApi is set up to be able to handle WIMs with multiple indexes, though the WIMs I usually work with only have a single one.
The way I understand my logic, there should only be one "info" object in "imageInfo", and thus run the loop only once. However, I'm getting over 30,000 lines returned (see here, too much for pastebin). Ironically, the last 15 lines are exactly how it is supposed to be.
If anybody could shed some light as to why in the heck it's doing this, and what I can do to fix it, I'd appreciate it. I have a feeling that it's something stupidly simple.
Code for Output
property:
public string Output
{
get { return output; }
set
{
if (!String.IsNullOrEmpty(value))
{
output += value;
}
}
}
Upvotes: 0
Views: 263
Reputation: 66489
So the reason I asked about Output
is I wondered if it was a simple string or a property with something else going on inside it. Your "setter" is messing you up.
Here's your property, from your comment:
public string Output
{
get { return output; }
set
{
if (!String.IsNullOrEmpty(value))
{
output += value;
}
}
}
First, you concatenate to Output
like this. If it were a simple string, all is well.
Output += String.Format(...);
But instead, thanks to +=
, you're basically doing this:
Output = Output + String.Format(...);
^^^^^^^^^^^^^^ // all of that is the "value" your passing into the property
In other words:
if (!String.IsNullOrEmpty(value))
{
// The value of "value" is everything that was in output,
// plus the additional string, which gets tacked on to the end of "output"
output += value;
}
To fix this, you've got to re-engineer this. For instance, remove one of the +=
either in your property setter, or in the foreach
loop.
I think I'd replace the property with a StringBuilder
:
var output = new StringBuilder();
output.AppendLine("Image information for image " + WimFile);
output.AppendLine("");
output.AppendLine("");
output.AppendLine(String.Format("Image index : {0}", info.ImageIndex.ToString()));
...
...
txtOutput.Text = output.ToString();
Upvotes: 2