Affichage des articles dont le libellé est c# file. Afficher tous les articles
Affichage des articles dont le libellé est c# file. Afficher tous les articles

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#


C# - Remove File And Directory

C# - How to Delete File And Folder Using C#

deete file or folder using c#


In This C# Tutorial  We Will See How To Delete File Or A Folder From A Path String Using CSharp Programming Language.


Project Source 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 WindowsFormsApplication1
{
    public partial class Form10 : Form
    {
        public Form10()
        {
            InitializeComponent();
        }

        private void BTN_RemoveFile_Click(object sender, EventArgs e)
        {
            string filepath = textBox1.Text;

            if (File.Exists(filepath))
            {

                File.Delete(filepath);
                MessageBox.Show("File Deleted");
                   
            }
            else
            {

                MessageBox.Show("File Not Deleted");

            }
        }

        private void BTN_RemoveFolder_Click(object sender, EventArgs e)
        {
            string folderepath = textBox2.Text;

            if (Directory.Exists(folderepath))
            {

                Directory.Delete(folderepath, true);
                MessageBox.Show("Folder Deleted");

            }
            else
            {
                MessageBox.Show("Folder Not Deleted");
            }
        }
    }
}




C# - File / Folder Exists

C# - How To Check If File / Directory Exists Using C#

c# file & directory exists


In This C# Tutorial  We Will See How To Verify If File Or A Folder Exists From A Path String Using CSharp Programming Language.


Project Source 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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string folderpath = textBox2.Text;

            if (Directory.Exists(folderpath))
            {
                label_Msg.Text = "Folder Exists";
                label_Msg.ForeColor = Color.Green;
            }
            else
            {
                label_Msg.Text = "Folder Not Exists";
                label_Msg.ForeColor = Color.Red;
            }
        }

        private void buttonCheck_Click(object sender, EventArgs e)
        {
            string filepath = textBox1.Text;

            if (File.Exists(filepath))
            {
                label_Msg.Text = "File Exists";
                label_Msg.ForeColor = Color.Green;
            }
            else
            {
                label_Msg.Text = "File Not Exists";
                label_Msg.ForeColor = Color.Red;
            }
        }
    }
}