2016-12-24 76 views
0

我有這個項目,您可以在其中拖放文件,並將所有項目添加到列表框中。爲什麼我的MessageBox不顯示任何文件擴展名?

然後它會提示您使用MessageBox詢問您是否希望看到擴展名。

當你按下「是」時,它應該會提示你一個MessageBox,告訴你每個文件有什麼文件擴展名,逐個循環,因此每個條目有1個messagebox,即「.txt」「.exe」「 pdf「等

但由於某種原因,它沒有顯示任何擴展,所以只是一個空白的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 
      { 

      } 
     } 
    } 
} 
+1

看看你的代碼:'串EXT = Path.GetExtension(文字);'你永遠只能初始化'文本'給一個空的字符串。 –

+0

這很有道理!謝謝! – JonnyKhanas

回答

1

應該item,而不是text

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

謝謝!這很好用! – JonnyKhanas

+0

@JonnyKhanas標記爲答案,如果它有幫助 – Sajeetharan

相關問題