I have code that is supposed to take all the user data that was input after the program was run and put it all into a text file.
Here is the code so far:
protected void WriteFile(object sender, EventArgs e)
{
FileStream fs = new FileStream(@"C:\Users\4567\MyDocuments\ExporterOutput.txt", FileMode.OpenOrCreate, FileAccess.Write);
TextBox[] tbs = { username, TextBox2, sgml, Path };
StringBuilder sb = new StringBuilder();
foreach (TextBox tb in tbs)
sb.AppendLine(tb.Text);
sb.AppendLine(DropDownList1.SelectedItem.ToString());
sb.AppendLine(DropDownList2.SelectedItem.ToString());
System.IO.File.WriteAllText(@"C:/Users/4567/My Documents/ExporterOutput.txt", sb.ToString());
I tried running it and the text file just shows up blank. Can anyone tell me what I am doing wrong and if there is an easier way to output all textbox information to a text file. And preferrable in a certain format.
Here is the edited code from the suggestions you gave me:
protected void WriteFile(object sender, EventArgs e)
{
TextBox[] tbs = { username, TextBox2, sgml, Path };
StringBuilder sb = new StringBuilder();
foreach (TextBox tb in tbs)
sb.AppendLine(tb.Text);
sb.AppendLine(DropDownList1.SelectedItem.ToString());
sb.AppendLine(DropDownList2.SelectedItem.ToString());
System.IO.File.WriteAllText(@"C:\\Users\\oZ012D\\My Documents\\ExporterOutput.txt", sb.ToString());
}
Ok, Im going to take a few guesses here.
Your last line should be
You are using windows, so you need the “\” char and not the forward slash like you would on Linux. This is accomplished by having the double backslash (which c# recognizes as a single backslash) in your string.
Also, try getting rid of your first line. You are creating a file stream and then not using or closing it. I think its possible that the open FS is causing issues.
Don’t worry about creation issues, the WriteAllText() call will create the file if it doesn’t exist and will overwrite it if it does.
Let me know if that works, the only other thing that looks potentially incorrect to me is the way you are getting text from the drop down menus.