C# Create And Write In Text File

How To Create And Write In A TXT File Using C#

create file in c#



In this C# Tutorial we will see How To Create A Text File From A Specific Path And Write Into It Text From A TextBox In CSharp Programming Language And Visual Studio IDE .




Project Source Code:

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

namespace WindowsFormsApplication1
{
    public partial class CreateFileAndWrite : Form
    {
        public CreateFileAndWrite()
        {
            InitializeComponent();
        }

        private void button_CreateAndWrite_Click(object sender, EventArgs e)
        {

            string path = "C:\\Users\\Desktop\\haha\\folder1\\myTextFile.txt";

            if (!File.Exists(path))
            {
                File.Create(path).Dispose();
                MessageBox.Show("File Cteated");

                StreamWriter sw = new StreamWriter(path);
                sw.WriteLine(textBox1.Text);

                sw.Close();
            }
            else
            {
                MessageBox.Show("File Already Exists");
            }

        }

    }
}
                                       

////// OUTPUT : 

write in text file using c#


Share this

Related Posts

Previous
Next Post »