如何逐个阅读文件夹中的.DAT文件

时间:2016-03-03 04:57:53

标签: c#

我正在创建一个软件来从.dat文件中获取数据并将这些数据写入数据库。有很多.dat文件,所以我需要进行批处理。我目前正在使用此代码

读取一个dat文件
        UDisk udisk = new UDisk();

        byte[] byDataBuf = null;
        int iLength;//length of the bytes to get from the data

        string sPIN2 = "";
        string sVerified = "";
        string sTime_second = "";
        string sDeviceID = "";
        string sStatus = "";
        string sWorkcode = "";

        openFileDialog1.Filter = "1_attlog(*.dat)|*.dat";
        openFileDialog1.FileName = "1_attlog.dat";//1 stands for one possible deviceid
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            FileStream stream = new FileStream(openFileDialog1.FileName, FileMode.OpenOrCreate, FileAccess.Read);
            byDataBuf = File.ReadAllBytes(openFileDialog1.FileName);
            iLength = Convert.ToInt32(stream.Length);

            lvSSRAttLog.Items.Clear();
            int iStartIndex = 0;
            int iOneLogLength;//the length of one line of attendence log
            for (int i = iStartIndex; i < iLength - 2; i++)//modify by darcy on Dec.4 2009
            {
                if (byDataBuf[i] == 13 && byDataBuf[i + 1] == 10)
                {
                    iOneLogLength = (i + 1) + 1 - iStartIndex;
                    byte[] bySSRAttLog = new byte[iOneLogLength];
                    Array.Copy(byDataBuf, iStartIndex, bySSRAttLog, 0, iOneLogLength);

                    udisk.GetAttLogFromDat(bySSRAttLog, iOneLogLength, out sPIN2, out sTime_second, out sDeviceID, out sStatus, out sVerified, out sWorkcode);

                    ListViewItem list = new ListViewItem();
                    list.Text = sPIN2;
                    list.SubItems.Add(sTime_second);
                    list.SubItems.Add(sDeviceID);
                    list.SubItems.Add(sStatus);
                    list.SubItems.Add(sVerified);
                    list.SubItems.Add(sWorkcode);
                    lvSSRAttLog.Items.Add(list);

                    bySSRAttLog = null;
                    iStartIndex += iOneLogLength;
                    iOneLogLength = 0;
                }
            }
            stream.Close();
        }

如何逐个读取文件并将这些数据传输到数据库中。

我的数据库是 - 短信    表是 -                        CHECKINOUT

          columns -CHECKDate    datetime    
                   CHECKTime    datetime    
                   InOutMode    int 
                   Status       varchar(50) 
                   WorkCode     varchar(50) 
                   Memoinfo     varchar(50) 
                   sn           int 
                   sn2          int 
                   Ip           varchar(16) 
                   Updated      int  

1 个答案:

答案 0 :(得分:1)

根据文件夹路径,您可以使用Directory.EnumerateFiles逐个获取.dat个文件:

string folder = @"D:\your\Folder";
foreach(var file in Directory.EnumerateFiles(
    folder,                             // folder path
    "*.dat"                             // file type you want
    /*, SearchOption.AllDirectories*/)) // uncomment it if you want open files in subfolder
{
    FileStream stream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Read);
    byDataBuf = File.ReadAllBytes(openFileDialog1.FileName);
    iLength = Convert.ToInt32(stream.Length);

    // Do your stuff here

    stream.Close();
}