FileRegion 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示要封存之檔案的區域。 此類別無法獲得繼承。
public ref class FileRegion sealed
public sealed class FileRegion
type FileRegion = class
Public NotInheritable Class FileRegion
- 繼承
-
FileRegion
範例
下列範例將示範如何使用 LogStore 和 FileRegion 類別,將記錄存放區封存至 XML。
class LogBackup
{
static void ArchiveToXML(LogStore logStore, string fileName)
{
LogArchiveSnapshot snapshot = logStore.CreateLogArchiveSnapshot();
{
XmlTextWriter writer = new XmlTextWriter(fileName, System.Text.Encoding.ASCII);
writer.WriteStartElement("logArchive");
foreach (FileRegion region in snapshot.ArchiveRegions)
{
writer.WriteStartElement("fileRegion");
writer.WriteElementString("path",
region.Path);
writer.WriteElementString("length",
region.FileLength.ToString());
writer.WriteElementString("offset",
region.Offset.ToString());
using (Stream dataStream = region.GetStream())
{
byte[] data = new byte[dataStream.Length];
dataStream.Read(data, 0, data.Length);
writer.WriteElementString("data", System.Convert.ToBase64String(data));
}
writer.WriteEndElement();
}
writer.Close();
logStore.SetArchiveTail(snapshot.LastSequenceNumber);
}
}
static void RestoreFromXML(string fileName)
{
using (XmlTextReader reader = new XmlTextReader(fileName))
{
reader.ReadStartElement("logArchive");
while (reader.IsStartElement())
{
string path;
long length;
long offset;
path = reader.ReadElementString("path");
length = System.Int64.Parse(reader.ReadElementString("length"));
offset = System.Int64.Parse(reader.ReadElementString("offset"));
string dataString = reader.ReadElementString("data");
byte[] data = System.Convert.FromBase64String(dataString);
FileStream fileStream;
using (fileStream = new FileStream(path,
FileMode.OpenOrCreate,
FileAccess.Write))
{
fileStream.SetLength(length);
fileStream.Position = offset;
fileStream.Write(data, 0, data.Length);
}
}
reader.ReadEndElement();
}
}
}
Friend Class LogBackup
Private Shared Sub ArchiveToXML(ByVal logStore As LogStore, ByVal fileName As String)
Dim snapshot As LogArchiveSnapshot = logStore.CreateLogArchiveSnapshot()
Dim writer As New XmlTextWriter(fileName, System.Text.Encoding.ASCII)
writer.WriteStartElement("logArchive")
For Each region As FileRegion In snapshot.ArchiveRegions
With writer
.WriteStartElement("fileRegion")
.WriteElementString("path", region.Path)
.WriteElementString("length", region.FileLength.ToString())
.WriteElementString("offset", region.Offset.ToString())
End With
Using dataStream As Stream = region.GetStream()
Dim data(dataStream.Length - 1) As Byte
dataStream.Read(data, 0, data.Length)
writer.WriteElementString("data", System.Convert.ToBase64String(data))
End Using
writer.WriteEndElement()
Next region
writer.Close()
logStore.SetArchiveTail(snapshot.LastSequenceNumber)
End Sub
Private Shared Sub RestoreFromXML(ByVal fileName As String)
Using reader As New XmlTextReader(fileName)
reader.ReadStartElement("logArchive")
Do While reader.IsStartElement()
Dim path As String
Dim length As Long
Dim offset As Long
path = reader.ReadElementString("path")
length = System.Int64.Parse(reader.ReadElementString("length"))
offset = System.Int64.Parse(reader.ReadElementString("offset"))
Dim dataString = reader.ReadElementString("data")
Dim data() = System.Convert.FromBase64String(dataString)
Dim fileStream As FileStream
fileStream = New FileStream(path, FileMode.OpenOrCreate, FileAccess.Write)
Using fileStream
fileStream.SetLength(length)
fileStream.Position = offset
fileStream.Write(data, 0, data.Length)
End Using
Loop
reader.ReadEndElement()
End Using
End Sub
End Class
備註
LogArchiveSnapshot 物件含有在 LogStore 中產生一致的資料備份所需的資訊。 實際的資料會包含在 FileRegion 屬性傳回之 ArchiveRegions 物件的可列舉集合中。 每個 FileRegion 執行個體代表必須封存之檔案中的位元組序列。
屬性
FileLength |
取得檔案的長度 (以位元組為單位)。 |
Offset |
取得檔案中資料起始位置的位移。 |
Path |
取得含有此區域之檔案的完整位置。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetStream() |
傳回資料流,此資料流可以用於讀取要封存的資料。 |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |