Why is my MessageBox not showing any file extension?

Question

I have this project where you can drag and drop files into it and it adds all the items to a listbox.

Then it prompts you with MessageBox asking if you would like to see the extension.

When you press "Yes" it should prompt you with a MessageBox telling you what file extension each and every file has, looping through them one by one so 1 messagebox per item saying i.e ".txt" ".exe" "pdf" etc.

but for some reason it's not showing any extensions what so ever just a blank MessageBox.

using System;
using System.IO;
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;

namespace dragndrop
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            AllowDrop = true;
            InitializeComponent();
        }

        private void frmMain_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
                e.Effect = DragDropEffects.Copy;
        }

        private void frmMain_DragDrop(object sender, DragEventArgs e)
        {

            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            lBox.Items.AddRange(files);

            DialogResult dr = MessageBox.Show("Would you like to see the extension?", "Option", MessageBoxButtons.YesNoCancel);
            if(dr == DialogResult.Yes)
            {

                string text = "";
                foreach (var item in lBox.Items)
                {
                    string ext = Path.GetExtension(text);
                    MessageBox.Show(ext);
                }
            }
            else
            {

            }
        }
    }
}

Show source
| c#   | wpf   | .net   | listbox   | winforms   2016-12-24 15:12 1 Answers

Answers to Why is my MessageBox not showing any file extension? ( 1 )

  1. 2016-12-24 15:12

    It should be item instead of text ,

     foreach (var item in lBox.Items)
     {
        string ext = Path.GetExtension(item);
        MessageBox.Show(ext);
     }
    

Leave a reply to - Why is my MessageBox not showing any file extension?

◀ Go back