Reputation: 4199
I am new to programming but I like it and I am trying to learn as much as possible. I am coding a small application that has a multiline text box (txtOutput) where I input my comments. These comments/lines are then transferred in an array of strings. I then call a class transferring some variable and also the array to print all out to a txt file. While I can get the txt file populated with the data from the variables, I cannot get the content of the array to the txt file. I would appreciate some help.
Text from txtOutput to array
public void button6_Click(object sender, EventArgs e)
{
string[] comments = txtOutput.Text.Split(' ');
Transfer of array and other variables to class1 (where I have the code to create and save the content of those variables and the array to txt file). The other two variables are printed out on the file without problems.
WriteTextFile WTR = new WriteTextFile();
WTR.Save(temperatura, speedMotore, comments);
Class1 code (where I do make the txt file and save it.
class WriteTextFile
{
public void Save(int sendTem, int sendMot, string[] comments)
{
DateTime Now = DateTime.Now;
string dateTime = Now.ToString("F");
SaveFileDialog sd = new SaveFileDialog();
sd.Filter = "Text File | *.txt";
if (sd.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(sd.FileName))
{
sw.Write("File diconfigurazione del programma Arduino Control " + "\r\n");
sw.Write("\r\n");
sw.Write(dateTime + "\r\n");
sw.Write("\r\n");
sw.Write("\r\n");
sw.Write("La temperatura impostata in PWM1 è: " + sendMot + "\r\n");
sw.Write("\r\n");
sw.Write("La velocità del motore impostata in PWM2 è: " + sendTem + "\r\n");
sw.Write("\r\n");
sw.Write(comments[1] + "\r\n");
sw.Dispose();
sw.Close();
}
}}}
I am not sure if I am transferring any text from the txtOutput to the "comments" array correctly. What I would like to achieve is to get those lines from the txtOutput printed on the txt file one by one and instead I just receive a message that says "Index out of range......."
Upvotes: 2
Views: 4460
Reputation: 4199
Problem solved. Thank you all for your comments. The array in my program works like this:
string[] comments = textBox5.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
then to print the content:
foreach (string i in comments)
{
sw.Write(i + "\r\n");
}
Upvotes: 0
Reputation: 498904
Do not call Dispose
as you do inside the using
statement - the using
statement will take care of that. This is likely the cause of the problem - the stream would dispose before flushing.
Just keep:
using (StreamWriter sw = new StreamWriter(sd.FileName))
{
sw.Write("File diconfigurazione del programma Arduino Control " + "\r\n");
sw.Write("\r\n");
sw.Write(dateTime + "\r\n");
sw.Write("\r\n");
sw.Write("\r\n");
sw.Write("La temperatura impostata in PWM1 è: " + sendMot + "\r\n");
sw.Write("\r\n");
sw.Write("La velocità del motore impostata in PWM2 è: " + sendTem + "\r\n");
sw.Write("\r\n");
sw.Write(comments[1] + "\r\n");
sw.Close();
}
Upvotes: 3