有人知道在C#中是否可以读取随机访问文件吗?
我正在尝试在C#中复制以下函数(来自旧的VB6应用程序)-
Open File For Random Shared As #100 Len = Len(Record)
Get #100, DM, Record
Close #100
Public DM As Long
Public Record As DMrecord
Public Type DMrecord
column1 As Long
column2 As Integer
column3 As Integer
column4 As Integer
column5 As String * 4
End Type
编辑 -
如下所建议,我已尝试使用VisualBasic DLL,并在FileGetObject行上收到以下错误 -
"Microsoft.VisualBasic.FileSystem.FileGetObject(int, ref object, long)的最佳重载方法存在一些无效参数"
我正在使用的代码是 -
public class Record
{
public int DMtype;
public long ecn;
public Record(int DMtype, long ecn)
{
this.DMtype = DMtype;
this.ecn = ecn;
}
public Record()
{
}
}
string fileName = @"C:\RandomAccess.dat";
string returnString = string.Empty;
int row = 1;
int maxRow = 1000;
Record aFileRecord = new Record();
FileSystem.FileOpen(1, fileName, OpenMode.Random, OpenAccess.Read, OpenShare.LockRead);
while (row < maxRow)
{
//Get record 2 1st.>>
FileSystem.FileGetObject(1, aFileRecord, row);
returnString += aFileRecord.DMtype.ToString() + "$" + aFileRecord.ecn.ToString();
row++;
}
FileSystem.FileClose(1);
我尝试将“记录”设置为结构体和类,但是遇到了相同的错误。
编辑22/08/13 - 我从未找到问题的根本原因,最终在VB6中将随机访问数据导出为逗号分隔文本文件,然后在SSIS中使用这些文件。
BinaryReader.ReadInt64()
读取长整型数据,BinaryReader.ReadInt32()
读取整型数据,而字符串将需要逐个读取。请按照我提供的链接中所述进行读取。点击这里开始阅读。 - Matthew WatsonBinaryReader.ReadInt32()
读取的长整型”。 - Matthew Watsonobject aFileRecord = new Record()
和FileSystem.FileGetObject(1, ref aFileRecord, row)
。 - jac