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));
}
}
Related
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();
}
}
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);
}
This program opens files into a textbox. It works fine with small files such as 4KB in size, but I am having trouble with a 200KB file. Ideally, I want to be able to open files of any size, but opening large files into a text box freezes the program. What am I doing wrong?
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
Text = openFileDialog1.FileName + " - " + "Fixprt";
textBox1.Text = String.Empty;
while (!sr.EndOfStream)
{
textBox1.Text += sr.ReadLine() + Environment.NewLine;
}
sr.Close();
}
openFileDialog1.Dispose();
}
Reading file line-by-line gives unwanted overhead. It would be better to read all file at once.
Consider to use async/await. This will bring you more responsive interface.
So I would suggest the next solution:
private async void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
Text = openFileDialog1.FileName + " - " + "Fixprt";
textBox1.Text = await sr.ReadToEndAsync();
sr.Close();
}
openFileDialog1.Dispose();
}
Edit
As discussed in the comments, this solution doesn't correctly process unix-style line breaks. For this case it can be other decision:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
Text = openFileDialog1.FileName + " - " + "Fixprt";
var sb = new StringBuilder();
while (!sr.EndOfStream)
{
sb.AppendLine(sr.ReadLine());
}
textBox1.Text = sb.ToString();
sr.Close();
}
openFileDialog1.Dispose();
}
Now we are using StringBuilder, which is designed for fast processing of string data.
This question already has answers here:
Open existing file, append a single line
(9 answers)
Closed 4 years ago.
Basicly I'm writing a fake malware, which will write in a .txt the Session username of the person which clicked on it. The problem is that when someone execute it will erase the previous lines.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "Fake Malware";
}
private void Form1_Load(object sender, EventArgs e)
{
string createText = " just clicked on the fake Malware";
File.WriteAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName + createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
private void zig(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("Link to a warning message on click, nothing important.");
string createText = " just clicked on the fake Malware";
File.WriteAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName+ createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
}
I think what you are looking for is AppendAllText:
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", "Wow it worked :)");
Here is the fix
private void Form1_Load(object sender, EventArgs e)
{
string createText = " just clicked on the fake Malware";
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName + createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
private void zig(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("#");
string createText = " just clicked on the fake Malware";
File.AppendAllText("//vm-files/users/ngallouj/zig/zig.txt", Environment.UserName+ createText + " " + DateTime.Now.ToString("h:mm:ss tt"));
string readText = File.ReadAllText("//vm-files/users/ngallouj/zig/zig.txt");
}
}
}
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!