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??
– Pekita
maybe adding commas would be better
– Pekita
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.
– user28595
@diegofm Thanks for the tip
– Pekita