加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在C#中读取随机访问文件

发布时间:2020-12-15 21:50:28 所属栏目:百科 来源:网络整理
导读:有谁知道是否有可能在C#中读取随机访问文件? 我试图在C#中复制以下函数(来自旧的VB6应用程序) – Open File For Random Shared As #100 Len = Len(Record) Get #100,DM,RecordClose #100Public DM As LongPublic Record As DMrecordPublic Type DMrecordcol
有谁知道是否有可能在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中的文件.

解决方法

只需添加对Microsoft.VisualBasic.dll的引用,并使用 FileSystem.FileOpen指定随机打开模式和 FileSystem.FileGetObject方法.这与VB6中的Open语句和Get关键字的行为相同.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读