net文件随机访问记录锁定

net文件随机访问记录锁定

本文介绍了.net文件随机访问记录锁定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要::在 .net 中,当将记录锁定在随机访问文件中时,我们无法访问文件中锁定记录之前的记录.

Summary: In .net When locking a record in a random access file we cannot access records ahead of the locked record in the file.

为了演示这个问题,我编写了两个简单的程序,一个打开并锁定记录,另一个尝试读取.

To demonstrate the issue I have written two simple programs one opens and locks a record and the other tries to read through.

结果是,当在第一个程序中锁定10条记录中的第9条记录时,我们能够读取记录1和2,但读取的记录就更多了!期望(这是我们对VB6的经验)是,您应该能够读取已锁定记录之外的所有记录.

The results are that when locking record number 9 out of the 10 in the first program we are able to read records 1 and 2 but no more! The expectation (And this is our experience with VB6) is that you should be able to read all the records apart from the one you have locked.

有人见过这个问题吗?我在做些奇怪的事吗?有什么解决方法吗?

Has anyone seen this problem? Am I doing something strange? Any work around?

演示代码:

程序1创建/打开/锁定

Sub Main()

    Dim FileName As String = "test.a"
    Dim ListofName() As String = {"Name1", "Name2", "Name3", "Name4",
            "Name5", "Name6", "Name7", "Name8", "Name9", "Name10"}

    Try

        Dim FileNumber1 As Integer = FreeFile()
        FileOpen(FileNumber1, FileName, OpenMode.Random,
                 OpenAccess.ReadWrite, OpenShare.Shared, 600)

        FileGet(FileNumber1, People, 1)

        'Create File if needs be
        If People.Name = "" Then
            For A = 1 To 10
                People.Name = ListofName(A - 1)
                FilePut(FileNumber1, People, A)
            Next
        End If

        'Lock the recoard we want for testing
        Lock(FileNumber1, 9)

    Catch ex As Exception
        FileClose()
    End Try
    FileClose()
End Sub

_

程序2打开并尝试阅读

Sub Main()

    Dim FileName As String = "C:\**Location of first program file**\test.a"

    Try

        Dim FileNumber1 As Integer = FreeFile()
        FileOpen(FileNumber1, FileName, OpenMode.Random,
                 OpenAccess.ReadWrite, OpenShare.Shared, 600)

        FileGet(FileNumber1, People, 2)

        'See  how much of the file we can read
        For A = 1 To 10
            FileGet(FileNumber1, People, A)
            System.Diagnostics.Debug.WriteLine(People.Name.ToString)
        Next

    Catch ex As Exception
        FileClose()
    End Try
    FileClose()
End Sub

编辑0.1:我们发现,文件中单个记录的锁定越深,在被锁定的记录之前,不可访问的字节/记录越多.

Edit 0.1: We have found that the deeper an individual record is locked within the file the more bytes/records are inaccessible before the locked one.

推荐答案

感谢您的评论.

在MSDN上发布了相同的问题此处,并设法获得了答案.

Posted the same question over on MSDN here and managed to get the answer.

但是在使用System.IO.FileStream.Read()并确切指定您不希望创建缓冲区的字节数时,允许您读取文件中除锁定记录以外的所有记录

But when using System.IO.FileStream.Read() and specifying exactly the number of bytes you want the buffer is not created allowing you to read all the records in a file except the locked one

代码示例:

<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)>
Structure Person
   <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=600)>
   <VBFixedString(600)>
   Dim name As String
End Structure

. . .

Using fs = New FileStream(FileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite, Marshal.SizeOf(People))
 For A = 1 To 10
  Try
     Console.WriteLine("Trying " & A & "...")

     Dim b() As Byte
     ReDim b(Marshal.SizeOf(People) - 1)
     fs.Seek((A - 1) * Marshal.SizeOf(People), SeekOrigin.Begin)
     fs.Read(b, 0, b.Length)
     Dim h = GCHandle.Alloc(b, GCHandleType.Pinned)
     People = Marshal.PtrToStructure(Of Person)(h.AddrOfPinnedObject())
     h.Free()
     Console.WriteLine(People.name.Trim())

  Catch ex As Exception
     Console.WriteLine("ERROR " & A & " " & ex.Message)
  End Try
 Next
End Using

这篇关于.net文件随机访问记录锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 18:59