【发布时间】:2013-04-30 16:37:12
【问题描述】:
有谁知道是否可以在 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);
我尝试将'Record'设置为结构和类,并得到相同的错误。
编辑 22/08/13 - 我从来没有深入了解这一点,最终将随机访问数据导出到 VB6 中的逗号分隔文本文件,然后在 SSIS 中使用这些文件。
【问题讨论】:
-
您必须使用 BinaryReader 自己解释数据。 See here to get started 可以用
BinaryReader.ReadInt64()读取长,用BinaryReader.ReadInt32()读取整数,一次读取一个字符串。按照我提供的链接中的说明阅读它们。 -
注意:vb6 Long 实际上是 int32
-
哎呀,所以我应该说“你可以用
BinaryReader.ReadInt32()阅读很长时间” -
只是猜测,
object aFileRecord = new Record()和FileSystem.FileGetObject(1, ref aFileRecord, row)
标签: c# asp.net vb6 vb6-migration randomaccessfile