有谁知道是否有可能读取C#中的随机访问文件?读取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自己解释数据。 [看到这里开始](http://stackoverflow.com/questions/4429829/read-variable-sized-string-from-binary-file-vb6-vs-c)长期你可以阅读'BinaryReader.ReadInt64( )',整数与'BinaryReader.ReadInt32()',你必须一次读取一个字符串。按照我给出的链接中的描述阅读它们。 – 2013-04-30 16:40:20
说明:vb6 Long实际上是int32 – 2013-04-30 16:45:29
哎呀,所以我应该说“Long你可以用'BinaryReader.ReadInt32()读取” – 2013-04-30 16:46:28