Edit .txt using stream and a button

0

I have a form in C # and winforms, and I need to, when I click a button, it edit 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);
                }
            }
        }
    }
}
    
asked by anonymous 05.08.2016 / 16:39

1 answer

1

Since no one answered I went searching and finally found out, in case someone has the same problem, here's the solution:

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

File.AppendAllText(caminho, string.Format("{0}{1}", "Texto", Environment.NewLine));
    
05.08.2016 / 17:37