Code:
private void btnSaveFile_Click(object sender, EventArgs e)
{
listBox1.MultiColumn = true;
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text File|*.txt";
if (sfd.ShowDialog() == DialogResult.OK) ;
{
string path = sfd.FileName;
BinaryWriter bw = new BinaryWriter(File.Create(path));
foreach (string s in hello)
{
bw.Write(s + Environment.NewLine);
}
}
output for
I really don’t know how to get rid of those characters.
If you want to write to a text file do not use a BinaryWriter. It appends the length of the string which explains why you are observing those strange characters.
You could use
TextWriter:or in this particular example you could shorten all this code to a single line of code using the
WriteAllLinesmethod:Also be careful! You have a trailing
;after yourifstatement. This means that the write to the file will always happen, no matter whether the user confirmed or canceled the file dialog. Notice how I removed it in my last example.