Как преобразовать код VBA (visual basic) в C #.
How can I read binary file ".DAT" to struct?
В моем коде vba:
Структура:
Option Explicit Type Security_File StockNo As Integer StockSymbol As String * 8 StockType As String * 1 Ceiling As Long Floor As Long BigLotValue As Double SecurityName As String * 25 SectorNo As String * 1 Designated As String * 1 SUSPENSION As String * 1 Delist As String * 1 HaltResumeFlag As String * 1 SPLIT As String * 1 Benefit As String * 1 Meeting As String * 1 Notice As String * 1 ClientIDRequired As String * 1 CouponRate As Integer IssueDate As String * 6 MatureDate As String * 6 AvrPrice As Long ParValue As Integer SDCFlag As String * 1 PriorClosePrice As Long PriorCloseDate As String * 6 ProjectOpen As Long OpenPrice As Long Last As Long LastVol As Long LastVal As Double Highest As Long Lowest As Long Totalshares As Double TotalValue As Double AccumulateDeal As Integer BigDeal As Integer BigVolume As Long BigValue As Double OddDeal As Integer OddVolume As Long OddValue As Double Best1Bid As Long Best1BidVolume As Long Best2Bid As Long Best2BidVolume As Long Best3Bid As Long Best3BidVolume As Long Best1Offer As Long Best1OfferVolume As Long Best2Offer As Long Best2OfferVolume As Long Best3Offer As Long Best3OfferVolume As Long BoardLost As Integer End Type
Файл чтения кода:
Private Sub ReadSecurity() Dim strFileName As String Dim intFileNum As Integer Dim strFileNameMar As String Dim intFileNumMar As Integer Dim record As Security_File Dim intRecordNum As Integer strFileName = "E:\Documents\MyData.DAT" intFileNum = FreeFile intRecordNum = 1 Open strFileName For Random Shared As intFileNum Len = Len(record) While Not EOF(intFileNum) Get intFileNum, intRecordNum, record With record If .StockType = "S" Or InStr(1, StrCodeStockView, Replace(Trim(.StockSymbol), "'", "")) <> 0 Then MsgBox (.StockType ) End If End With intRecordNum = intRecordNum + 1 Wend Close #intFileNum I don't know how to convert vb to c #. End Sub
Что я уже пробовал:
Я попробовал это сделать, но данные не верны.
Мой код c# :
using (BinaryReader b = new BinaryReader(File.OpenRead(@"E:\Documents\MyData.DAT"))) { for (int i = 0; i < b.BaseStream.Length; ++i) { try { var a1 = b.ReadString(); var t1 = b.ReadInt64(); var a2 = b.ReadSingle(); var a3 = b.ReadSingle(); var a4 = b.ReadSingle(); var a5 = b.ReadSingle(); var a6 = b.ReadSingle(); var a7 = b.ReadSingle(); var a8 = b.ReadSingle(); var a9 = b.ReadSingle(); //streamWriter.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", a1, a2, a3, a4, a5, a6, a7, a8, a9, t1)); Console.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", a1, a2, a3, a4, a5, a6, a7, a8, a9, t1)); Console.WriteLine("==================="); } catch { continue; } } }
BillWoodruff
Как вы сериализовали данные в VB: покажите этот код.
tvtoanitvn
Я получаю данные автоматически. На пути старого программного обеспечения.
Свяжите мой файл:
https://filebin.net/qkzt4vox0b9as3ji/SECURITY.DAT?t=1kic11ux
BillWoodruff
Я думаю, вы обнаружите, что мы неохотно открываем неизвестный файл из соображений безопасности. Добавьте более конкретную информацию к вашему вопросу.