1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#region
 
using System.IO;
 
#endregion
 
namespace CSFrameworkV5.Common
{
    /// <summary>
    ///     取真实的文件类型(文件扩展名)
    /// </summary>
    public class TrueFileFormat
    {
        /// <summary>
        ///     取真实的文件类型
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static FileExtension GetExtension(string path)
        {
            if (!File.Exists(CodeSafeHelper.GetSafePath(path)))
                return FileExtension.UNKNOW;
 
            var fs = new FileStream(CodeSafeHelper.GetSafePath(path),
                FileMode.Open, FileAccess.Read);
            var r = new BinaryReader(fs);
            var bx = "";
            byte buffer;
            try
            {
                buffer = r.ReadByte();
                bx = buffer.ToStringEx();
                buffer = r.ReadByte();
                bx += buffer.ToStringEx();
                r.Close();
                fs.Close();
                return (FileExtension)int.Parse(bx); //真实的文件类型
            }
            catch
            {
                return FileExtension.UNKNOW;
            }
        }
    }
 
    /// <summary>
    ///     文件扩展名编号
    /// </summary>
    public enum FileExtension
    {
        UNKNOW = 0, //未知类型
        JPG = 255216,
        GIF = 7173,
        BMP = 6677,
        PNG = 13780,
        COM = 7790,
        EXE = 7790,
        DLL = 7790,
        RAR = 8297,
        ZIP = 8075,
        XML = 6063,
        HTML = 6033,
        ASPX = 239187,
        CS = 117115,
        JS = 119105,
        TXT = 210187,
        SQL = 255254,
        BAT = 64101,
        BTSEED = 10056,
        RDP = 255254,
        PSD = 5666,
        PDF = 3780,
        CHM = 7384,
        LOG = 70105,
        REG = 8269,
        HLP = 6395,
        DOC = 208207,
        XLS = 208207,
        DOCX = 208207,
        XLSX = 208207
    }
}