FileRecordSequence 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 FileRecordSequence 類別的新執行個體。
多載
FileRecordSequence(String) |
使用指定的檔案,初始化 FileRecordSequence 類別的新執行個體。 |
FileRecordSequence(String, FileAccess) |
使用指定的檔案和存取模式初始化 FileRecordSequence 類別的新執行個體。 |
FileRecordSequence(String, FileAccess, Int32) |
使用指定的檔案、存取模式和檔案大小初始化 FileRecordSequence 類別的新執行個體。 |
FileRecordSequence(String)
使用指定的檔案,初始化 FileRecordSequence 類別的新執行個體。
public:
FileRecordSequence(System::String ^ path);
public FileRecordSequence (string path);
new System.IO.Log.FileRecordSequence : string -> System.IO.Log.FileRecordSequence
Public Sub New (path As String)
參數
- path
- String
此 FileRecordSequence 執行個體所要封裝之檔案的相對或絕對路徑。
例外狀況
path
指定的檔案無效。
找不到 path
指定的檔案。
因為意外的 I/O 例外狀況,所以無法執行要求。
沒有足夠的記憶體可以繼續執行程式。
存取指定的記錄存放區遭作業系統拒絕。
範例
下列範例會建立記錄序列、將記錄附加至該序列、讀取記錄,最後處置序列。
public class MyLog
{
string logName = "test.log";
FileRecordSequence sequence = null;
bool delete = true;
public MyLog()
{
// Create a FileRecordSequence.
sequence = new FileRecordSequence(logName, FileAccess.ReadWrite);
}
// Append records to the record sequence.
public void AppendRecords()
{
Console.WriteLine("Appending Log Records...");
SequenceNumber previous = SequenceNumber.Invalid;
previous = sequence.Append(CreateData("Hello World!"), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush);
previous = sequence.Append(CreateData("This is my first Logging App"), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush);
previous = sequence.Append(CreateData("Using FileRecordSequence..."), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush);
Console.WriteLine("Done...");
}
// Read the records added to the log.
public void ReadRecords()
{
Encoding enc = Encoding.Unicode;
Console.WriteLine();
Console.WriteLine("Reading Log Records...");
try
{
foreach (LogRecord record in this.sequence.ReadLogRecords(this.sequence.BaseSequenceNumber, LogRecordEnumeratorType.Next))
{
byte[] data = new byte[record.Data.Length];
record.Data.Read(data, 0, (int)record.Data.Length);
string mystr = enc.GetString(data);
Console.WriteLine(" {0}", mystr);
}
}
catch (Exception e)
{
Console.WriteLine("Exception {0} {1}", e.GetType(), e.Message);
}
Console.WriteLine();
}
// Dispose the record sequence and delete the log file.
public void Cleanup()
{
// Dispose the sequence.
sequence.Dispose();
// Delete the log file.
if (delete)
{
try
{
File.Delete(this.logName);
}
catch (Exception e)
{
Console.WriteLine("Exception {0} {1}", e.GetType(), e.Message);
}
}
}
// Converts the given data to an Array of ArraySegment<byte>
public static IList<ArraySegment<byte>> CreateData(string str)
{
Encoding enc = Encoding.Unicode;
byte[] array = enc.GetBytes(str);
ArraySegment<byte>[] segments = new ArraySegment<byte>[1];
segments[0] = new ArraySegment<byte>(array);
return Array.AsReadOnly<ArraySegment<byte>>(segments);
}
}
class LogSample
{
static void Main2(string[] args)
{
MyLog log = new MyLog();
log.AppendRecords();
log.ReadRecords();
log.Cleanup();
}
}
Public Class MyLog
Private logName As String = "test.log"
Private sequence As FileRecordSequence = Nothing
Private delete As Boolean = True
Public Sub New()
' Create a FileRecordSequence.
sequence = New FileRecordSequence(logName, FileAccess.ReadWrite)
End Sub
' Append records to the record sequence.
Public Sub AppendRecords()
Console.WriteLine("Appending Log Records...")
Dim previous As SequenceNumber = SequenceNumber.Invalid
previous = sequence.Append(CreateData("Hello World!"), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush)
previous = sequence.Append(CreateData("This is my first Logging App"), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush)
previous = sequence.Append(CreateData("Using FileRecordSequence..."), SequenceNumber.Invalid, SequenceNumber.Invalid, RecordAppendOptions.ForceFlush)
Console.WriteLine("Done...")
End Sub
' Read the records added to the log.
Public Sub ReadRecords()
Dim enc As Encoding = Encoding.Unicode
Console.WriteLine()
Console.WriteLine("Reading Log Records...")
Try
For Each record In Me.sequence.ReadLogRecords(Me.sequence.BaseSequenceNumber, LogRecordEnumeratorType.Next)
Dim data(record.Data.Length - 1) As Byte
record.Data.Read(data, 0, CInt(Fix(record.Data.Length)))
Dim mystr As String = enc.GetString(data)
Console.WriteLine(" {0}", mystr)
Next record
Catch e As Exception
Console.WriteLine("Exception {0} {1}", e.GetType(), e.Message)
End Try
Console.WriteLine()
End Sub
' Dispose the record sequence and delete the log file.
Public Sub Cleanup()
' Dispose the sequence.
sequence.Dispose()
' Delete the log file.
If delete Then
Try
File.Delete(Me.logName)
Catch e As Exception
Console.WriteLine("Exception {0} {1}", e.GetType(), e.Message)
End Try
End If
End Sub
' Converts the given data to an Array of ArraySegment<byte>
Public Shared Function CreateData(ByVal str As String) As IList(Of ArraySegment(Of Byte))
Dim enc As Encoding = Encoding.Unicode
Dim array() As Byte = enc.GetBytes(str)
Dim segments(0) As ArraySegment(Of Byte)
segments(0) = New ArraySegment(Of Byte)(array)
Return System.Array.AsReadOnly(Of ArraySegment(Of Byte))(segments)
End Function
End Class
Friend Class LogSample
Private Shared Sub Main2(ByVal args() As String)
Dim log As New MyLog()
log.AppendRecords()
log.ReadRecords()
log.Cleanup()
End Sub
End Class
備註
這個建構函式 (Constructor) 會建立檔案的讀取/寫入權限,並使用共用的讀取權限開啟檔案。 這表示,在這個 FileRecordSequence 執行個體已處置之前,這個或其他處理序要開啟檔案以執行寫入的要求都會失敗,但嘗試讀取則會成功。 如果找不到 path
,會建立 0 位元組大小的新檔案。
適用於
FileRecordSequence(String, FileAccess)
使用指定的檔案和存取模式初始化 FileRecordSequence 類別的新執行個體。
public:
FileRecordSequence(System::String ^ path, System::IO::FileAccess access);
public FileRecordSequence (string path, System.IO.FileAccess access);
new System.IO.Log.FileRecordSequence : string * System.IO.FileAccess -> System.IO.Log.FileRecordSequence
Public Sub New (path As String, access As FileAccess)
參數
- path
- String
此 FileRecordSequence 執行個體所要封裝之檔案的相對或絕對路徑。
- access
- FileAccess
有效的 FileAccess 值,可控制使用者對記錄檔擁有的存取類型。
例外狀況
path
指定的檔案無效。
找不到 path
指定的檔案。
因為意外的 I/O 例外狀況,所以無法執行要求。
沒有足夠的記憶體可以繼續執行程式。
存取指定的記錄存放區遭作業系統拒絕。
適用於
FileRecordSequence(String, FileAccess, Int32)
使用指定的檔案、存取模式和檔案大小初始化 FileRecordSequence 類別的新執行個體。
public:
FileRecordSequence(System::String ^ path, System::IO::FileAccess access, int size);
public FileRecordSequence (string path, System.IO.FileAccess access, int size);
new System.IO.Log.FileRecordSequence : string * System.IO.FileAccess * int -> System.IO.Log.FileRecordSequence
Public Sub New (path As String, access As FileAccess, size As Integer)
參數
- path
- String
此 FileRecordSequence 執行個體所要封裝之檔案的相對或絕對路徑。
- access
- FileAccess
有效的 FileAccess 值,可控制使用者對記錄檔擁有的存取類型。
- size
- Int32
要開啟之記錄檔的大小。
例外狀況
一或多個參數超出有效範圍。
path
指定的檔案無效。
找不到 path
指定的檔案。
因為意外的 I/O 例外狀況,所以無法執行要求。
沒有足夠的記憶體可以繼續執行程式。
存取指定的記錄存放區遭作業系統拒絕。