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

时间: 2024-08-27 05:27:43

C#获取文件类型的相关文章

delphi 动态获取文件类型的图标

delphi 动态获取文件类型的图标.txt我不奢望什么,只希望你以后的女人一个不如一个.真怀念小时候啊,天热的时候我也可以像男人一样光膀子!在应用程序的编写中,组合框(ComboBox).列表框(ListBox).等常见的部件,通常不仅要用于显示文字,而且还要显示其与文字相关的图标.在一般的Windows应用程序中,这些图标的显示都要随列出的显示文本的变化而变化,例如在组合框中列出当前目录下的所有文件时,在组合框左边就显示与文件名相关联的图标,这就是所谓的动态图标.在 Delphi中使用动态图

C#文件拖放至窗口的ListView控件获取文件类型

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 F

用SHGetFileInfo获取文件类型/图标信息

SHFILEINFO fileInfo; SHGetFileInfo( "C:\\boot.ini", NULL, &fileInfo, sizeof(SHFILEINFO), SHGFI_ICON|SHGFI_TYPENAME); cout<<fileInfo.szTypeName<<"文件"<<endl; cout<<"0x"<<hex<<fileInfo.hIc

C#文件拖放至窗体的ListView控件获取文件类型

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 F

Java获取文件类型Mime Type的各种方法

http://chinacheng.iteye.com/blog/857485 http://www.rgagnon.com/javadetails/java-0487.html 谢谢以上的博主

finfo_open函数不支持,通过二进制流获取文件类型

public function getMime() { // $finfo = finfo_open(FILEINFO_MIME_TYPE); // return finfo_file($finfo, $this->filename); $fp=fopen($this->filename,'r');///实际是image/png $bin = fread($fp, 2); //只读2字节 fclose($fp); $str_info = @unpack("C2chars",

获取pe文件的文件类型

工程文件petype.cpp通过调用pefile类中的函数获取文件类型. 文件类型的判断通过5个监测点完成. 监测点1:dos头的e_magic 监测点2:nt头的Signature 监测点3:文件头的Characteristics 监测点4:可选头的Magic 监测点5:可选头的Subsystem 通过监测点1和2判断是否是pe文件: 通过监测点3判断文件是否是动态库文件 通过监测点4判断文件是pe32还是pe32+还是rom映像 通过监测点5判断文件是否是0环可执行文件[驱动文件],还是3环

第九篇:使用 lstat 函数获取文件信息

前言 在之前的文章中,描述过如何用 fcntl 函数改变文件的状态标记.但,文件还有很多信息,如文件类型,权限设置,设备编号,访问时间等等.如果要获取这些信息,则使用函数 lstat 可以轻松达到这个目的. 下面的程序将使用该函数获取文件类型信息并判断它的类型. 但在这之前,先要说明文件信息结构体和文件类型判定宏. 文件信息结构体 调用 lstat 函数将会把指定文件的信息存放到 stat 结构体中,这个结构体的定义大致如下: 文件类型判定宏 文件结构体中的文件类型字段是一个整数,对此,可以使用

java通过文件头来判断文件类型

import java.io.FileInputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map.Entry; /** * @Description 根据的文件头来判断文件类型 * @author LJ * @Version v1.0 */ public class GetFileTypeByHead { public static void main(String[] args)