MLyons10

I have several textBoxes in my application, and would like to save the data in the textBoxes to a .txt file, but I would like to add white space.

As an example:

Company:
abc Company

Name:
Joe Smith

Address:
123 Main St.
Mytown, mystate myzip

Does anyone happen to know how to write the data in the textBoxes to a .txt file (The application needs to create the .txt file as well), and create the necessary white space

Any help and information is greatly appreciated.

Thanks,



Re: Visual C# Express Edition Save data to .txt file with white space?

Traps

Look up system.io namespace. You need to create a filestream.



Re: Visual C# Express Edition Save data to .txt file with white space?

jrboddie

Here is an example:

Code Snippet

private void button1_Click(object sender, EventArgs e)

{

if (saveFileDialog1.ShowDialog()==DialogResult.OK)

{

using (FileStream fs = new FileStream(saveFileDialog1.FileName,FileMode.Create ))

{

using (StreamWriter w = new StreamWriter(fs,Encoding.ASCII))

{

w.WriteLine("Company:");

w.WriteLine(" " + textBox1.Text ); //White space + string

w.WriteLine(""); //Blank line

w.WriteLine("Name:");

w.WriteLine(" "+ textBox2.Text );

}

}

}

}

You will need to also add:

using System.IO;





Re: Visual C# Express Edition Save data to .txt file with white space?

MLyons10

Thank you very much for your response, I will play around with this and see if I can get it to work.

One question I did have was, will this bring up the SaveFileDialog (And do I need to add that to my application) I would like to save the .txt file without using the SaveFileDialog if possible. The file will always be saved in the same directory, and will always be named the same thing.

Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition Save data to .txt file with white space?

jrboddie

Yes, my example uses the saveFile dialog which you would have to add, but of course you don't have to use the dialog if you have already determined a filename. I presume you can find where the filename needs to go in as a string.





Re: Visual C# Express Edition Save data to .txt file with white space?

MLyons10

T hank you very much for your help. I tried the following code:

private void button1_Click(object sender, EventArgs e)
{
using (StreamWriter w = new StreamWriter(fs, Encoding.ASCII))
{
w.WriteLine("Company:");
w.WriteLine(" " + textBox1.Text);
w.WriteLine(""); //Blank line
w.WriteLine("Name:");
w.WriteLine(" " + textBox20.Text);
w.WriteLine(""); //Blank line
w.WriteLine("Address:");
w.WriteLine(" " + textBox7.Text);
w.WriteLine(" " + textBox9.Text);
w.WriteLine(" " + textBox10.Text + ", " + comboBox1.Text + " " + textBox16.Text);
w.WriteLine(""); //Blank line
w.WriteLine("Phone:");
w.WriteLine(" " + maskedTextBox1.Text);
w.WriteLine(" " + maskedTextBox3.Text);

//Save the TXT Document
txtDoc.Save(@"c:\project files\" + textBox1.Text "\Info.txt");
}

I looked at the code for saving an xml document as well as what you had posted above. VS is throwing two errors though, one is that it does not recognize the reference to "fs", which I tried including and not including as I would prefer not to use a SaveFileDialog. The second error is that it does not recognize the "txtDoc.Save", which I figured would be similar to the xmlDoc.Save used for saving xml files. I didn't realize C# and VS would handle a txt document so differently.

What would I use in place of the xmlDoc.Save, and how would I go about getting this to work Basically, I would like for the user, when they click this button to automatically save the information to a .txt file in the location specified.

Any help and information is greatly appreciated.

Thanks Again,

















Re: Visual C# Express Edition Save data to .txt file with white space?

jrboddie

You left out the line that created fs. You still need a filestream. In the place for the filename, put your own path (as a string.)





Re: Visual C# Express Edition Save data to .txt file with white space?

MLyons10

Thank you very much, that works great. One final question though, I have earlier in my code (In the Leave Event of another textBox) that the program creates a folder named whatever is in that textBox. I would like to save this file in this folder, but when I try to put the code like so:

using (FileStream fs = new FileStream(@"c:\projects\" + textBox1.Text + "\Info.txt", FileMode.Create))

It throws an error and says that it is an unrecognized escape sequence. I'm not sure why it doesn't like the last "\". With it being between quotes, I thought it would be handled as text, and not looked at as code. How would I be able to do this

Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition Save data to .txt file with white space?

jrboddie

The '\' character is used for escape sequences. To override that function, use the '@' character before your literal string like this:

using (FileStream fs = new FileStream(@"c:\projects\" + textBox1.Text + @"\Info.txt", FileMode.Create))





Re: Visual C# Express Edition Save data to .txt file with white space?

MLyons10

Thank you very much, I will remember that. I never used escape sequences, so I wasn't sure what that was even trying to do...

I really appreciate all of your help.

One final question (I'm very sorry to bother you gain). The .txt file is adding a blank space to the end of the .txt document, and because of how the document is used, this can in some circumstances be a problem. I was wondering if there was a simple way to remove this additional blank space. I tried to search for an answer to this, but couldn't find anything about removing blank lines.

Additionally, I have one textBox (Address2), which may not always have data in it, am I able to setup the code to not include that line if it is empty I don't know if this is possible, but was just thinking about it. I will see if I can find reference for this as well.

Any help and information is greatly appreciated.

Thanks Again,




Re: Visual C# Express Edition Save data to .txt file with white space?

jrboddie

If you want the last line to end without a newline added, then use

w.Write( ... );

instead of

w.WriteLine( .... );

To optionally write a textBox, you could do something like this:

Code Snippet

if (textBox9.TextLength>0)

{

w.WriteLine(" " + textBox9.Text);

}





Re: Visual C# Express Edition Save data to .txt file with white space?

MLyons10

Thank you very much for your help. I greatly appreciate it. This is working great.

Thanks Again,