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;
            }
        }
    }
}

Share this

Related Posts

Previous
Next Post »