Edit . txt using stream and a button

Asked

Viewed 65 times

0

I have a form in c# and winforms, and I need that, when I click a button, it edits a . txt and add a line of text.

Follow the commented code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace Scratch
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        //close form
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //btnWhenClicked
        private void btnWhenStart_Click(object sender, EventArgs e)
        {
            ListItemsBox.Items.Add("When Start");
            btnWhenStart.Hide();
            string path = @"C:\Users\Estagio\Desktop\MyTest.txt";

            //Create And Write File
            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("using System;");
                    sw.WriteLine("using System.Collections.Generic;");
                    sw.WriteLine("using System.Linq;");
                    sw.WriteLine("using System.Text;");
                    sw.WriteLine("using System.Threading.Tasks;");

                    sw.WriteLine("\r\n namespace CMD");
                    sw.WriteLine("{");
                    sw.WriteLine("    class Program");
                    sw.WriteLine("    {");
                    sw.WriteLine("        static void Main(string[] args)");
                    sw.WriteLine("        {");

                }
            }


        }

        //delete ListItemsBox Selected Item
        private void ListItemsBox_DoubleClick(object sender, EventArgs e)
        {  
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this item?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dialogResult == DialogResult.Yes)
            {
                ListItemsBox.Items.Remove(ListItemsBox.SelectedItem);
            }
            else if (dialogResult == DialogResult.No)
            {

            }
        }

        private void btnStringEdit_Click(object sender, EventArgs e)
        {
            //É aqui que eu quero que ele edit o .txt
            ListItemsBox.Items.Add("String");
            string path = @"C:\Users\Estagio\Desktop\MyTest.txt";

            if (!File.Exists(path))
            {
                using (StreamWriter sw = File.CreateText(path))
                {
                    //Adicionando esta linha
                        sw.WriteLine("String " + StringNameTxtBox);
                }
            }
        }
    }
}
  • @diegofm??

  • maybe adding commas would be better

  • All right, edited out. I am not a moderator, I am trying to help you to elaborate questions with a clearer and less confusing text, because this is the fourth question of yours that I see, where the text seems more like a jumble of words only. A well-written question attracts upvotes and good answers.

  • @diegofm Thanks for the tip

1 answer

1


Since no one answered I went to search and finally found out, in case someone has the same problem here is the solution:

string caminho = AppDomain.CurrentDomain.BaseDirectory.ToString() + "MyTest.txt";

File.AppendAllText(caminho, string.Format("{0}{1}", "Texto", Environment.NewLine));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.