C# save file so that the output is multi-lines - c#

I'm trying my first attempt at a "Save file" button.
It's going surprisingly well. :D
I have been able to get the file to combine multiple pieces of user-inputted text from different text-boxes into a single file and save it as a text file output.
Where I am stuck is that I'm trying to figure out how to split the text file into multiple lines for easier readability. I have tried searching for examples online, but everything I'm seeing shows the opposite, how to combine multiple text inputs into a single file. I've already got that.
Here's my code. I'm trying to break it into new lines where the long empty spaces are.
Thanks in advance.
private void button1_Click_2(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text File|*.txt";
sfd.FileName = "Writing Prompt- " + Txt_Prompt_Title.Text;
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = sfd.FileName;
TextWriter sf = new StreamWriter (File.Create(path));
sf.Write("Writing Prompt: " + Txt_Prompt_Title.Text + " " + Lbl_Prompt_Output.Text + " " + Txt_Prompt_Notes.Text);
sf.Dispose();
}
}

This is simply a restatement of the OP's self-answer, incorporating the suggestions that were made in the comments. I'm also using the orginal using syntax, not everyone is using a recent compiler. If the OP updates his/her answer, I'll delete this:
private void button1_Click_2(object sender, EventArgs e)
{
using (var sfd = new SaveFileDialog()) {
sfd.Filter = "Text File|*.txt";
sfd.FileName = "Writing Prompt- " + Txt_Prompt_Title.Text;
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = sfd.FileName;
using (var sf = new StreamWriter (File.Create(path))){
sf.Write("Writing Prompt: " + Txt_Prompt_Title.Text +
"\n" +
"\n " + Lbl_Prompt_Output.Text +
"\n" +
"\n " + Txt_Prompt_Notes.Text);
}
}
}
}
What I didn't add was code that uses Environment.Newline instead of "\n", or code that gets rid of the string concatenation (which can cause a lot of garbage string objects that need to be garbage collected). One way to achieve both would be to use .WriteLine over and over again, as #MathieuGuindon suggests).
You probably want to look up string interpolation for creating strings that have the string rendering of other objects embedded in them.
You should really follow the link to the using statement that #jimi has pointed you to. It provides a way to make sure you properly Dispose of objects that implement IDisposable while making your code more readable.

I figured it out.
private void button1_Click_2(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text File|*.txt";
sfd.FileName = "Writing Prompt- " + Txt_Prompt_Title.Text;
if (sfd.ShowDialog() == DialogResult.OK)
{
string path = sfd.FileName;
TextWriter sf = new StreamWriter (File.Create(path));
sf.Write("Writing Prompt: " + Txt_Prompt_Title.Text +
"\n" +
"\n " + Lbl_Prompt_Output.Text +
"\n" +
"\n " + Txt_Prompt_Notes.Text);
sf.Dispose();
}
}

Related

I have a problem in saving file with the FolderBrowserDialog

I have this code:
private void button1_Click(object sender, EventArgs e)
{
string x = "Name: " + label1.Text + " " + "FamilyName: " + label2.Text + " " + "FatherName: " + label3.Text + " " + "PhoneNumber: " + label4.Text;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
if (radioButton1.Checked == true)
{
System.IO.File.WriteAllText(folderBrowserDialog1.SelectedPath + label1.Text + ".txt", x);
MessageBox.Show("The file registered.");
}
else if (radioButton2.Checked == true)
{
System.IO.File.WriteAllText(folderBrowserDialog1.SelectedPath + label1.Text + ".doc", x);
MessageBox.Show("The file registered.");
}
else
{
MessageBox.Show("Please choose one of the formats.");
}
}
}
And this is for store some information from labels in a file, and dynamically selects the file name from label1. Then I put a radio button to choose between saving the file in txt format or doc format. After selecting one of the formats and clicking on the Save button, the folderBrowserDialog opens so I can choose the path I want to save my file there. But when I chose my path, let's say that I chose this path: 'G:\SavingFile\TextFiles', instead of saving the file in the TextFiles folder, saves it in the SavingFile folder.
My question is why it doesn't save the file in the last folder? And how can I fix it?
You should not concatenate your paths as string but use System.IO.Path.Combine instead. as this will also take care of the correct path-separators, which are missing from your code as SelectedPath does not end with a path-separator
So in your case
var filepath = System.IO.Path.Combine(folderBrowserDialog1.SelectedPath, label1.Text + ".txt");
System.IO.File.WriteAllText(filepath, ....);
This will take care of the required path separators.
If you are using .Net Core you might also use System.IO.Path.Join. But be aware there are some differences in the behaviour of these two methods with regards to rooting the resulting path. See the linked docs for details.
I also found another way to save my files that is easier than the way that I did above. I can simply use saveFileDialog.
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.File.WriteAllText(saveFileDialog1.FileName, x);
}

How to append text to my file so it adds new content instead of overwrite

Everything is working until I register a new user and click "save" it overwrites whats in the text file with the new info, I would like to add a new line and start to add the contents array again. I know there is an "append" function but I've tried many things and cant seem to get it to work with my current way of writing to a file.
private async void button1_Click(object sender, EventArgs e)
{
string[] contents = new string[4];
contents[0] = "Name: " + txtName.Text;
contents[1] = "Address: " + txtAddress.Text;
contents[2] = "Phone: " + txtPhone.Text;
contents[3] = "Blood Type: " + cmbBloodType.SelectedItem.ToString();
System.IO.File.WriteAllLines( #"C:\Users\Ben\Documents\C#\Final\Bloodbank\BloodBank\bin\Debug\Bloodbank.txt", contents);
}
I think it's because your content is an array, so you have to join this array in one string like:
System.IO.File.AppendAllText(#"C:\Users\Ben\Documents\C#\Final\Bloodbank\BloodBank\bin\Debug\Bloodbank.txt", string.Join(Environment.NewLine, contents));
Have you tried File.AppendAllText()?
Here is an example:
File.AppendAllText(#"C:\Users\Ben\Documents\C#\Final\Bloodbank\BloodBank\bin\Debug\Bloodbank.txt", "content" + Environment.NewLine);
Edited Code: When you go to save your file, try:
private async void button1_Click(object sender, EventArgs e)
{
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
stringBuilder.AppendLine("Name: " + txtName.Text);
stringBuilder.AppendLine("Address: " + txtAddress.Text);
stringBuilder.AppendLine("Phone: " + txtPhone.Text);
stringBuilder.AppendLine("Blood Type: " + cmbBloodType.SelectedItem.ToString());
System.IO.File.AppendAllText(#"C:\Users\Ben\Documents\C#\Final\Bloodbank\BloodBank\bin\Debug\Bloodbank.txt", stringBuilder.ToString() + Environment.NewLine);
}
That should append the text to the end of the file. The official documentation can be found here: https://msdn.microsoft.com/en-us/library/ms143356(v=vs.110).aspx
Hope this helps!

How to save text font (which was set up manually) with a text in C#

I set up the font manually for the labels, however, when I am saving it as a Word Document the font which I set up previously disappears. I do not know how to figure it out
private void button1_Click(object sender, EventArgs e)
{
string text = label1.Text + textBox1.Text + "\r\n\r\n\r\n" +
label2.Text + textBox2.Text + "\r\n\r\n\r\n";
sSaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Microsoft Word| *.doc";
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path = sfd.FileName;
MessageBox.Show(path);
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(text);
}
}
}
}
StreamWriter (basically) writes a string (of characters) to a file. Word formatting is not that simple. If you want the formatting then it gets more complicated.
See this MSDN article for more info on formatting Word documents. You need an object that can control the document.

Saving multiple pieces of form input to a file

What would be the best way to save a windows form to a file with a few text boxes collecting user input. I using this at the moment:
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, tB1.Text);
File.WriteAllText(saveFileDialog1.FileName, tB2.Text);
}
This is fine for saving the input from the first text box but when it comes to the other it wont save the data entered.
If it was me I would use the StreamWriter / StreamReader Classes since they have WriteLine and Readline methods respectively.
i.e. something like this
private void button1_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
{
sw.WriteLine(tB1.Text);
sw.WriteLine(tB2.Text);
sw.WriteLine(tB3.Text);
sw.WriteLine(tB4.Text);
sw.Close();
}
}
}
private void button2_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
{
tB1.Text = sr.ReadLine();
tB2.Text = sr.ReadLine();
tB3.Text = sr.ReadLine();
tB4.Text = sr.ReadLine();
sr.Close();
}
}
}
Concatenate this two texbox then;
File.WriteAllText(saveFileDialog1.FileName, tB1.Text + Environment.NewLine + tB2.Text );
how about concatenating the two textboxes? for clarity,
string forSaving = tB1.Text + "\n" + tB2.Text;
File.WriteAllText(saveFileDialog1.FileName, forSaving);
or
File.WriteAllText(saveFileDialog1.FileName, tB1.Text + "\n" + tB2.Text);
UPDATE 1
string firstName = "FirstName: " + txtFirstName.Text;
string lastName = "LastName: " + txtLastName.Text;
string personAddress = "FirstName: " + txtAddress.Text;
string details = firstName + "\n" + lastName + "\n" + personAddress;
File.WriteAllText(saveFileDialog1.FileName, tB1.Text + "\n" + details);
The best way would probably be to create a method in your form that will return a string with all of the values from the TextBoxes into whatever format you want. Something like this would work:
File.WriteAllText(saveFileDialog1.fileName, OutputUserInfo());
Then inside OutputUserInfo() you can have whatever formatting to the data that you want so you can understand what they put in.
Edit Example of OutputUserInfo()
private string OutputUserInfo() {
return "First Name: " + tbFirstName.Text + Environment.NewLine +
"Surname: " + tbSurname.Text + Environment.NewLine +
"Address" + tbAddress.Text + Environment.NewLine;
// Just keep adding whatever you want on here.
// Add the descriptions if you want, it will probably help
}
You could also have this in different formats (CSV, or whatever). But if you're just doing a plain text file, this could be easiest. It is up to you though.
File.WriteAllText is probably bad because it overwrites your content.
Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
Instead go with File.AppendAllText which
Appends the specified stringto the file, creating the file if it does not already exist.
There we go, use Encoding to Append all the string.
private void button1_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
string line = string.Format("{0},{1}"
, textBox1.Text
, textBox2.Text);
File.AppendAllText(saveFileDialog1.FileName, line, Encoding.GetEncoding(1252));
}
}

C# WinForms - Error on OpenFileDialog MultiSelect: "Index was outside the bounds of the array."

In debug mode, while running the C# WinFOrms App, After I select the files through the OpenFileDialog, I get the
Error: Could not read file from disk.
Original error: Index was outside the bounds of the array.
Do you have any idea on how to fix this Error?
Here's my code:
// When the user clicks on Select Files Button, this happens
private void sourceFiles_Click(object sender, EventArgs e)
{
Stream myStream;
int i = 0;
OpenFileDialog sourceFileOpenFileDialog = new OpenFileDialog();
this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\";
this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|" + "All Files (*.*)|*.*";
this.sourceFileOpenFileDialog.FilterIndex = 2;
this.sourceFileOpenFileDialog.RestoreDirectory = true;
this.sourceFileOpenFileDialog.Multiselect = true;
this.sourceFileOpenFileDialog.Title = "Please Select Excel Source File(s) for Consolidation";
if (this.sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
string tempFolder = System.IO.Path.GetTempPath();
foreach (string FileName in this.sourceFileOpenFileDialog.FileNames)
{
this.sourceFileOpenFileDialog.FileNames[i] = FileName;
listBoxSourceFiles.Items.Add(FileName);
Log("Source Files: " + sourceFileOpenFileDialog.FileNames[i]);
i++;
System.IO.File.Copy(FileName, tempFolder + #"\" + FileName);
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
//method for the sourcefileOpenFileDialog. Do I need anything here?
private void sourceFileOpenFileDialog_FileOk(object sender, CancelEventArgs e)
{
}
//method for the listbox. Do I need anything here?
private void listBoxSourceFiles_SelectedIndexChanged(object sender, EventArgs e)
{
}
Thanks!
What you are doing doesn't seem to make a lot of sense. What is the following line supposed to do?
this.sourceFileOpenFileDialog.FileNames[i] = FileName;
Just change your foreach to this:
foreach (string FileName in this.sourceFileOpenFileDialog.FileNames)
{
listBoxSourceFiles.Items.Add(FileName);
Log("Source Files: " + FileName);
System.IO.File.Copy(FileName, Path.Combine(tempFolder, Path.GetFileName(FileName)));
}
The error arises from the fact, that you have two variables named sourceFileOpenFileDialog. One is a member of your class and one is declared inside the method.
The one that is declared inside the method is only ever used in the following line:
Log("Source Files: " + sourceFileOpenFileDialog.FileNames[i]);
Because this instance is not used to show the dialog to the user, its FileNames property has a Length of 0 and therefore trying to access any items in it results in the exception.
Update:
There is one more problem:
FileName is a complete path, so appending it to the temp path will result in an invalid path. Also, consider using Path.Combine to combine two paths:
Path.Combine(tempFolder, Path.GetFileName(FileName))

Categories