1. 程式人生 > >C#文件拖放至窗口的ListView控件獲取文件類型

C#文件拖放至窗口的ListView控件獲取文件類型

space headers listview http track csdn effect agent pac

技術分享

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FileStyle
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Data_List(ListView LV, string F) 
        {
            string Format = "";
            if (F.LastIndexOf(".") == F.Length - 4)
                Format = F.Substring(F.LastIndexOf(".") + 1, 3);
            ListViewItem item = new ListViewItem(F);
            item.SubItems.Add(Format);
            LV.Items.Add(item);
        }

        private void listView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;	
            String[] str_Drop = (String[])e.Data.GetData(DataFormats.FileDrop, true);//必須用字符串數組
            Data_List(listView1, str_Drop[0]);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            listView1.GridLines = true;
            listView1.View = View.Details;//顯示列名稱
            listView1.FullRowSelect = true;
            listView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;//隱藏列標題
            listView1.Columns.Add("文件名稱", listView1.Width - 65, HorizontalAlignment.Right);
            listView1.Columns.Add("類型", 60, HorizontalAlignment.Center);
        }
    }
}


C#文件拖放至窗口的ListView控件獲取文件類型