C#如何通过文件流前2个字节确定文件类型
1、打开VS,选择Visual C#模板创建WinForm应用程序。输入一个自己满意的解决方案名称,并指定其位置,点击确认按钮进入代码编写界面。
2、在Form1.Designer.cs页面中找到partial class Form1{……}整段代码,即图中框选区域代码,用下面代码进行替换。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.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(103, 83); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 0; this.label1.Text = "label1"; // // Form1 // this.AllowDrop = true; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(239, 198); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Form1"; this.DragDrop += new System.Windows.Forms.DragEventHandler(this.Form1_DragDrop); this.DragEnter += new System.Windows.Forms.DragEventHandler(this.Form1_DragEnter); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; }
3、在Form1页面中找到public partial class Form1 : Form{……}整段代码,即图中框选区域代码,用下面代码进行替换。 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.All; else e.Effect = DragDropEffects.None; } private void Form1_DragDrop(object sender, DragEventArgs e) { string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false); string fileName = s[0]; System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read); byte[] bs = new byte[2]; fs.Read(bs, 0, 2); fs.Close(); int n = bs[0] * 256 + bs[1]; switch (n) { case 255*256+216: this.label1.Text = ".jpg"; break ; case 208 * 256 + 207: this.label1.Text = ".doc .xls .ppt .wps"; break; case 80 * 256 + 75: this.label1.Text = ".docx .pptx .xlsx .zip"; break; case 82 * 256 + 97: this.label1.Text = ".rar"; break; case 77 * 256 + 90: this.label1.Text = ".exe .dll"; break; case 9552: this.label1.Text = ".pdf"; break; case 71 * 256 + 73: this.label1.Text = ".gif"; break; case 137 * 256 + 80: this.label1.Text = ".png"; break; case 66 * 256 + 77: this.label1.Text = ".bmp"; break; case 56 * 256 + 66: this.label1.Text = ".psd"; break; case 60 * 256 + 63: this.label1.Text = ".xml"; break; case 60 * 256 + 33: this.label1.Text = ".htm .html"; break; case 64 * 256 + 101: this.label1.Text = ".bat"; break; default : this.label1.Text = n.ToString(); break; } } }
4、编译代码,生成解决方案。
5、编译成功后,按F5执行,拖动一个文件放置到软件窗口中,窗口将显示文件类型,如果文件类型不在当前代码统计中,则会显示前2个字节数值,一般可以将其添加到软件中。