Display File Name and Path in ListBox Using C# Windows Application

Hi programmers, welcome to new article of c# windows application. This article i’ll write program to Get File Name and Path in ListBox Using C# Windows Application.
We can achieve the output OpenFileDialog class and v1 object. v1 call ShowDialog method. when v1.ShowDialog method equals to Cancel then condition true. v1 call filename and stored into string object s. s i.e filename and path add into listbox.
let’s see the codes.

Double Click on Button and Write Below Codes.

Use below codes, Directly into Editor.

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        List<string> str = new List<string>();

        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog v1 = new OpenFileDialog();  
          
            if (v1.ShowDialog() == DialogResult.Cancel)
                return;

            foreach (string s in v1.FileNames)
            {
                listBox1.Items.Add(Path.GetFileName(s));
                listBox1.Items.Add(Path.GetFullPath(s));
                str.Add(s);
            }
        }
    }
}

Happy Coding…Thanks.

Post Author: adama