Hi,
I made an arraylist and put there 10 txt files. And i got 10 checkboxes.
I want to bind the checkboxes (which are also in an array type of CheckBox)
with the corresponding index of the arraylist.
And after that i want to merge all the files into one file.
This is the code:
private CheckBox[] checkBox = new CheckBox[10];
public void Method()
{
ArrayList fileArray = new ArrayList();
string file1 = File.ReadAllText("file1.txt");
string file2 = File.ReadAllText("file2.txt");
string file3 = File.ReadAllText("file3.txt");
string file4 = File.ReadAllText("file4.txt");
string file5 = File.ReadAllText("file5.txt");
string file6 = File.ReadAllText("file6.txt");
string file7 = File.ReadAllText("file7.txt");
string file8 = File.ReadAllText("file8.txt");
string file9 = File.ReadAllText("file9.txt");
string file10 = File.ReadAllText("file10.txt");
fileArray.Add(file1);
fileArray.Add(file2);
fileArray.Add(file3);
fileArray.Add(file4);
fileArray.Add(file5);
fileArray.Add(file6);
fileArray.Add(file7);
fileArray.Add(file8);
fileArray.Add(file9);
fileArray.Add(file10);
foreach (Control chkBx in this.gbxLanguageLongSound.Controls)
{
if (chkBx is CheckBox)
{
for (int i = 0; i < 10; i++)
{
CheckBox currentCheckBox = (CheckBox)this.checkBox["checkbox" + (i+1)];
if (currentCheckBox.Checked == true)
{
File.WriteAllText("CombinedFiles.txt", filesOfCheckedBoxes);
}
}
}
}
I hope i made myself clear.
Thanks in advance!

merging multiple files into 1
zeifer
Whenever a class implements the IDisposable interface you should call the Dispose method when you don't need the object anymore.
The using statement will do this for you, even if an Exception accore! The documentation about the using statement can be found here: using statement (c#).
perstam
Hi,
Its okay, no problem. It works.
I've never seen "using" somewhere in the middle of a code like this before:
using (StreamReader reader = File.OpenText("doc.txt"))
I thought that its only importing using directives at the top of the file.
Can you explain that "using" please
sGurpreet
I got it!
Thank you very much for your time and patience!
MickJ
As far as I understood your problem, here is the self explaining code. For each checkbox you check if it is checked and if it is, append the text to the (merge)file:
public void Method()
{
ArrayList fileArray = new ArrayList();
using (StreamWriter writer = File.CreateText(@"c:\allfiles.txt"))
{
if (checkBox1.Checked)
{
using (StreamReader reader = File.OpenRead("c:\file1.txt"))
{
writer.Write(reader.ReadToEnd());
}
}
if (checkBox2.Checked)
{
using (StreamReader reader = File.OpenRead("c:\file2.txt"))
{
writer.Write(reader.ReadToEnd());
}
}
if (checkBox3.Checked)
{
using (StreamReader reader = File.OpenRead("c:\file3.txt"))
{
writer.Write(reader.ReadToEnd());
}
}
}
}
Mr Pro Tools
Hi,
Thank you for your reply.
That looks like a better solution than i had.
I got 3 errors regarding this lines:
using (StreamReader reader = File.OpenRead("c:\file1.txt"))
using (StreamReader reader = File.OpenRead("c:\file2.txt"))
using (StreamReader reader = File.OpenRead("c:\file3.txt"))
This is the error:
Cannot implicitly convert type 'System.IO.FileStream' to 'System.IO.StreamReader'.
How can i fix that
Thanks in advance!
barkingdog
bn.srinivasa rao