C#獲取文件類型


Form1.cs

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

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

        public void Data_List(ListView LV, string F)  //Form或MouseEventArgs添加命名空間using System.Windows.Forms;
        {
            string enlarge = "";
            if (F.LastIndexOf(".") == F.Length - 4)
            {
                enlarge = F.Substring(F.LastIndexOf(".") + 1, 3);
            }
            ListViewItem item = new ListViewItem(F);
            item.SubItems.Add(enlarge);
            LV.Items.Add(item);
        }

        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);//設置頭像
        }
    }
}

Form1.Designer.cs

namespace FileStyle
{
    partial class Form1
    {
        /// <summary>
        /// 必需的設計器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設計器生成的代碼

        /// <summary>
        /// 設計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.listView1 = new System.Windows.Forms.ListView();
            this.SuspendLayout();
            //
            // listView1
            //
            this.listView1.AllowDrop = true;
            this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.listView1.Location = new System.Drawing.Point(0, 0);
            this.listView1.Name = "listView1";
            this.listView1.Size = new System.Drawing.Size(292, 253);
            this.listView1.TabIndex = 0;
            this.listView1.UseCompatibleStateImageBehavior = false;
            this.listView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.listView1_DragEnter);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 253);
            this.Controls.Add(this.listView1);
            this.Name = "Form1";
            this.Text = "獲取文件的類型";
            this.Shown += new System.EventHandler(this.Form1_Shown);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.ListView listView1;
    }
}

Program.cs

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

namespace FileStyle
{
    static class Program
    {
        /// <summary>
        /// 應用程序的主入口點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM