本文整理汇总了VB.NET中System.IO.FileStream.Lock方法的典型用法代码示例。如果您正苦于以下问题:VB.NET FileStream.Lock方法的具体用法?VB.NET FileStream.Lock怎么用?VB.NET FileStream.Lock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream 的用法示例。
在下文中一共展示了FileStream.Lock方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。
示例1: FStreamLock
' 导入命名空间
Imports System.IO
Imports System.Text
Public Class FStreamLock
Shared Sub Main()
Dim uniEncoding As New UnicodeEncoding()
Dim lastRecordText As String = _
"The last processed record number was: "
Dim textLength As Integer = _
uniEncoding.GetByteCount(lastRecordText)
Dim recordNumber As Integer = 13
Dim byteCount As Integer = _
uniEncoding.GetByteCount(recordNumber.ToString())
Dim tempString As String
Dim aFileStream As New FileStream( _
"Test#@@#.dat", FileMode.OpenOrCreate, _
FileAccess.ReadWrite, FileShare.ReadWrite)
Try
' Write the original file data.
If aFileStream.Length = 0 Then
tempString = _
lastRecordText + recordNumber.ToString()
aFileStream.Write(uniEncoding.GetBytes(tempString), _
0, uniEncoding.GetByteCount(tempString))
End If
' Allow the user to choose the operation.
Dim consoleInput As Char = "R"C
Dim readText(CInt(aFileStream.Length)) As Byte
While consoleInput <> "X"C
Console.Write(vbcrLf & _
"Enter 'R' to read, 'W' to write, 'L' to " & _
"lock, 'U' to unlock, anything else to exit: ")
tempString = Console.ReadLine()
If tempString.Length = 0 Then
Exit While
End If
consoleInput = Char.ToUpper(tempString.Chars(0))
Select consoleInput
' Read data from the file and
' write it to the console.
Case "R"C
Try
aFileStream.Seek(0, SeekOrigin.Begin)
aFileStream.Read( _
readText, 0, CInt(aFileStream.Length))
tempString = New String( _
uniEncoding.GetChars( _
readText, 0, readText.Length))
Console.WriteLine(tempString)
recordNumber = Integer.Parse( _
tempString.Substring( _
tempString.IndexOf(":"C) + 2))
' Catch the IOException generated if the
' specified part of the file is locked.
Catch ex As IOException
Console.WriteLine("{0}: The read " & _
"operation could not be performed " & _
"because the specified part of the" & _
" file is locked.", _
ex.GetType().Name)
End Try
Exit Select
' Update the file.
Case "W"C
Try
aFileStream.Seek(textLength, _
SeekOrigin.Begin)
aFileStream.Read( _
readText, textLength - 1, byteCount)
tempString = New String( _
uniEncoding.GetChars( _
readText, textLength - 1, byteCount))
recordNumber = _
Integer.Parse(tempString) + 1
aFileStream.Seek( _
textLength, SeekOrigin.Begin)
aFileStream.Write(uniEncoding.GetBytes( _
recordNumber.ToString()), 0, byteCount)
aFileStream.Flush()
Console.WriteLine( _
"Record has been updated.")
' Catch the IOException generated if the
' specified part of the file is locked.
Catch ex As IOException
Console.WriteLine( _
"{0}: The write operation could " & _
"not be performed because the " & _
"specified part of the file is " & _
"locked.", ex.GetType().Name)
End Try
Exit Select
' Lock the specified part of the file.
Case "L"C
Try
aFileStream.Lock(textLength - 1, byteCount)
Console.WriteLine("The specified part " & _
"of file has been locked.")
Catch ex As IOException
Console.WriteLine( _
"{0}: The specified part of file " & _
"is already locked.", _
ex.GetType().Name)
End Try
Exit Select
' Unlock the specified part of the file.
Case "U"C
Try
aFileStream.Unlock( _
textLength - 1, byteCount)
Console.WriteLine("The specified part " & _
"of file has been unlocked.")
Catch ex As IOException
Console.WriteLine( _
"{0}: The specified part of file " & _
"is not locked by the current " & _
"process.", ex.GetType().Name)
End Try
Exit Select
' Exit the program.
Case Else
consoleInput = "X"C
Exit While
End Select
End While
Finally
aFileStream.Close()
End Try
End Sub
End Class
开发者ID:VB.NET开发者,项目名称:System.IO,代码行数:146,代码来源:FileStream.Lock
示例2: Module1
' 导入命名空间
Imports System.IO
Module Module1
Sub Main()
Dim FileSt As FileStream = New FileStream("test.dat", FileMode.Create, FileAccess.Write, FileShare.Write)
Try
FileSt.Lock(0, 100)
Console.WriteLine("Locked")
Catch Ex As Exception
Console.WriteLine(Ex.Message)
End Try
Try
FileSt.Unlock(0, 100)
Console.WriteLine("Unlocked")
Catch Ex As Exception
Console.WriteLine(Ex.Message)
End Try
Dim Values As Byte()
Values = New Byte() {1, 2, 3, 4, 5}
Try
FileSt.Seek(0, SeekOrigin.Begin)
FileSt.Write(Values, 0, 5)
Console.WriteLine("Successfully updated file")
Catch Ex As Exception
Console.WriteLine(Ex.Message)
End Try
End Sub
End Module
开发者ID:VB程序员,项目名称:System.IO,代码行数:36,代码来源:FileStream.Lock
注:本文中的System.IO.FileStream.Lock方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论