C#读写通达信股票软件二进制dat文件

代码如下:

/// <summary>
        /// 添加自定义数据
        /// </summary>
        /// <param name="file_path">文件路径,此文件为二进制dat文件</param>
        /// <returns></returns>
        private string AddSignal(string file_path)
        {
            var result = ""; // 执行结果

            // 如果文件不存在则创建
            if (!File.Exists(file_path))
            {
                using (FileStream file = new FileStream(file_path, FileMode.Create))
                {

                }
            }

            // 数据结构说明
            // 数据号(4)+类型(4)+数据(48)+校验位(4)
            // 类型:外部 00 00 00 00 序列 01 00 00 00
            // 数据:48位,每一个十进制数占两位,每个十进制数独立分割存储
            // 校验位:校验算法不清楚,已测试过Modbus校验、LRC校验、和校验、异或校验,皆不是。

            // 读取历史文件数据
            var history_data = "";
            FileStream fs = new FileStream(file_path, FileMode.Open);
            byte[] array = new byte[fs.Length];
            fs.Read(array, 0, array.Length);
            fs.Close();
            for (int i = 0; i < array.Length; i++)
            {
                history_data = history_data + string.Format("{0:X2}", array[i]);
            }

            var hex_data = ""; // 需要写入的十六进制数据
            // 过滤已存在的数据
            int[] IDs = { 901, 9881, 9882, 9883, 9884, 9885, 9886, 9995, 9996, 9997, 9701, 9603 };
            for (int i = 0; i < IDs.Length; i++)
            {
                var id = IDs[i];
                // 十进制转十六进制存储&小端形式
                var hex_id = "";
                byte[] byteId = BitConverter.GetBytes(id);
                if (BitConverter.IsLittleEndian)
                {
                    // 如果是小端字节序,则不需要调整  
                }
                else
                {
                    // 如果是大端字节序,则反转字节数组以得到小端字节序  
                    Array.Reverse(byteId);
                }
                hex_id = BitConverter.ToString(byteId).Replace("-", "");

                // 封装数据
                var column_data = "";
                #region
                if (id == 901)
                {
                    column_data = "8503000000000000C8FDD4C2C4DABDE2BDFBB9C9000000000000000000000000000000000000000000000000000000000000000000000000F0D2F50D";
                }
                if (id == 9881)
                {
                    column_data = "9926000000000000C1ACB0E2CCECCAFD00000000000000000000000000000000000000000000000000000000000000000000000000000000D011F80D";
                }
                if (id == 9882)
                {
                    column_data = "9A26000000000000CAD7B7E2CAB1BCE400000000000000000000000000000000000000000000000000000000000000000000000000000000E8D1F50D";
                }
                if (id == 9883)
                {
                    column_data = "9B26000000000000C4A9B7E2CAB1BCE4000000000000000000000000000000000000000000000000000000000000000000000000000000009012F80D";
                }
                if (id == 9884)
                {
                    column_data = "9C26000000000000B8F6B9C9D0C2CEC500000000000000000000000000000000000000000000000000000000000000000000000000000000D87EFF0D";
                }
                if (id == 9885)
                {
                    column_data = "9D26000000000000B0E5BFE9CCE2B2C400000000000000000000000000000000000000000000000000000000000000000000000000000000E8D1F50D";
                }
                if (id == 9886)
                {
                    column_data = "9E26000000000000CDACBBA8CBB3B8C5C4EE00000000000000000000000000000000000000000000000000000000000000000000000000001812F80D";
                }
                if (id == 9995)
                {
                    column_data = "0B27000000000000CCD8CAE2CCE1CABE000000000000000000000000000000000000000000000000000000000000000000000000000000009012F80D";
                }
                if (id == 9996)
                {
                    column_data = "0C27000000000000D6F7D3AAD2B5CEF100000000000000000000000000000000000000000000000000000000000000000000000000000000D87EFF0D";
                }
                if (id == 9997)
                {
                    column_data = "0D27000000000000D5C7CDA3D4ADD2F200000000000000000000000000000000000000000000000000000000000000000000000000000000687FFF0D";
                }
                if (id == 9701)
                {
                    column_data = "E525000000000000D3CED7CAD0ADD7F7CFAFCEBB3937303100000000000000000000000000000000000000000000000000000000000000005013F80D";
                }
                if (id == 9603)
                {
                    column_data = "8325000000000000BEBABCDBB1CACAFD000000000000000000000000000000000000000000000000000000000000000000000000000000003813F80D";
                }
                #endregion

                // 只有不存在的数据号才需要加入
                if (!history_data.Contains(hex_id))
                {
                    hex_data += column_data;
                }
            }

            // 添加数据至二进制文件dat
            try
            {
                if (!string.IsNullOrEmpty(hex_data))
                {
                    // 写入数据
                    hex_data = hex_data.Replace(",", "");
                    hex_data = hex_data.Replace("\n", "");
                    hex_data = hex_data.Replace("\\", "");
                    hex_data = hex_data.Replace(" ", "");

                    if (hex_data.Length % 2 != 0)
                    {
                        hex_data += "20";//空格
                    }

                    byte[] bytes_data = new byte[hex_data.Length / 2];
                    for (int j = 0; j < bytes_data.Length; j++)
                    {
                        try
                        {
                            bytes_data[j] = byte.Parse(hex_data.Substring(j * 2, 2),
                            System.Globalization.NumberStyles.HexNumber);
                        }
                        catch (Exception ex)
                        {
                            result = ex.Message;
                        }
                    }

                    if (string.IsNullOrEmpty(history_data))
                    {
                        // 如果历史数据为空,则创建
                        BinaryWriter bw = new BinaryWriter(new FileStream(file_path, FileMode.Create));
                        bw.Write(bytes_data);
                        bw.Close();

                        result = "自定义数据写入成功";
                    }
                    else
                    {
                        // 如果历史数据存在,则追加
                        BinaryWriter bw = new BinaryWriter(new FileStream(file_path, FileMode.Append));
                        bw.Write(bytes_data);
                        bw.Close();

                        result = "自定义数据追加成功";
                    }
                }
                else
                {
                    result = "数据重复,无需更新";
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 添加自定义板块
        /// </summary>
        /// <param name="file_path">文件路径,此文件为二进制dat文件</param>
        /// <returns></returns>
        private string AddBlock(string file_path)
        {
            var result = ""; // 执行结果

            // 如果文件不存在则创建
            if (!File.Exists(file_path))
            {
                using (FileStream file = new FileStream(file_path, FileMode.Create))
                {

                }
            }

            // 数据结构说明
            // 数据号(4)+类型(4)+数据(48)+校验位(4)
            // 类型:外部 00 00 00 00 序列 01 00 00 00
            // 数据:48位,每一个十进制数占两位,每个十进制数独立分割存储
            // 校验位:校验算法不清楚,已测试过Modbus校验、LRC校验、和校验、异或校验,皆不是。

            // 读取历史文件数据
            var history_data = "";
            FileStream fs = new FileStream(file_path, FileMode.Open);
            byte[] array = new byte[fs.Length];
            fs.Read(array, 0, array.Length);
            fs.Close();
            for (int i = 0; i < array.Length; i++)
            {
                history_data = history_data + string.Format("{0:X2}", array[i]);
            }

            var hex_data = ""; // 需要写入的十六进制数据
            // 人气榜
            var top_data = "C8CBC6F8B0F1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000052514200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
            // 竞价笔数
            var bid_data = "BEBABCDBB1CACAFD0000000000000000000000000000000000000000000000000000000000000000000000000000000000004A4A4253000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";

            // 只有不存在才需要加入
            if (!history_data.Contains("C8CBC6F8B0F100"))
            {
                hex_data += top_data;
            }
            if (!history_data.Contains("BEBABCDBB1CACAFD00"))
            {
                hex_data += bid_data;
            }

            // 添加数据至二进制文件dat
            try
            {
                if (!string.IsNullOrEmpty(hex_data))
                {
                    // 写入数据
                    hex_data = hex_data.Replace(",", "");
                    hex_data = hex_data.Replace("\n", "");
                    hex_data = hex_data.Replace("\\", "");
                    hex_data = hex_data.Replace(" ", "");

                    if (hex_data.Length % 2 != 0)
                    {
                        hex_data += "20";//空格
                    }

                    byte[] bytes_data = new byte[hex_data.Length / 2];
                    for (int j = 0; j < bytes_data.Length; j++)
                    {
                        try
                        {
                            bytes_data[j] = byte.Parse(hex_data.Substring(j * 2, 2),
                            System.Globalization.NumberStyles.HexNumber);
                        }
                        catch (Exception ex)
                        {
                            result = ex.Message;
                        }
                    }

                    if (string.IsNullOrEmpty(history_data))
                    {
                        // 如果历史数据为空,则创建
                        BinaryWriter bw = new BinaryWriter(new FileStream(file_path, FileMode.Create));
                        bw.Write(bytes_data);
                        bw.Close();

                        result = "板块写入成功";
                    }
                    else
                    {
                        // 如果历史数据存在,则追加
                        BinaryWriter bw = new BinaryWriter(new FileStream(file_path, FileMode.Append));
                        bw.Write(bytes_data);
                        bw.Close();

                        result = "板块追加写入成功";
                    }
                }
                else
                {
                    result = "数据重复,无需更新";
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
            }

            return result;
        }

运行效果:

DEMO软件下载:

https://download.csdn.net/download/staroldmanx/89130152

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值