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) |