Udostępnij za pośrednictwem


FileRegion.GetStream Metoda

Definicja

Zwraca strumień, który może służyć do odczytywania danych do zarchiwizowania.

public:
 System::IO::Stream ^ GetStream();
public System.IO.Stream GetStream ();
member this.GetStream : unit -> System.IO.Stream
Public Function GetStream () As Stream

Zwraca

Strumień zawierający dane do zarchiwizowania.

Wyjątki

Nie można wykonać żądania z powodu nieoczekiwanego wyjątku we/wy.

Operacja jest nieobsługiwana.

Metoda została wywołana po usunięciu sekwencji.

Za mało pamięci, aby kontynuować wykonywanie programu.

Dostęp do określonego magazynu dzienników jest odrzucany przez system operacyjny.

Przykłady

W poniższym przykładzie pokazano, jak zarchiwizować magazyn dzienników w formacie XML przy użyciu LogStore klas i FileRegion .

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

Dotyczy