Package.Open Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Apre un pacchetto.
Overload
Open(Stream) |
Apre un pacchetto su un flusso di IO specificato. |
Open(String) |
Apre un pacchetto in un percorso e con nome file specificati. |
Open(Stream, FileMode) |
Apre un pacchetto con un flusso di IO e modalità file specificati. |
Open(String, FileMode) |
Apre un pacchetto in un percorso specificato utilizzando una modalità di file specificata. |
Open(Stream, FileMode, FileAccess) |
Apre un pacchetto con un'impostazione specificata di flusso di IO, modalità file e accesso ai file. |
Open(String, FileMode, FileAccess) |
Apre un pacchetto in un percorso specificato utilizzando un'impostazione specificato di modalità file e accesso ai file. |
Open(String, FileMode, FileAccess, FileShare) |
Apre un pacchetto in un percorso specificato utilizzando un'impostazione specificata di modalità file, accesso ai file e condivisione file. |
Esempio
Nell'esempio seguente viene illustrato come creare un nuovo Package oggetto che incorpora PackageRelationship e PackagePart gli elementi insieme ai dati archiviati.
// -------------------------- CreatePackage --------------------------
/// <summary>
/// Creates a package zip file containing specified
/// content and resource files.</summary>
private static void CreatePackage()
{
// Convert system path and file names to Part URIs. In this example
// Uri partUriDocument /* /Content/Document.xml */ =
// PackUriHelper.CreatePartUri(
// new Uri("Content\Document.xml", UriKind.Relative));
// Uri partUriResource /* /Resources/Image1.jpg */ =
// PackUriHelper.CreatePartUri(
// new Uri("Resources\Image1.jpg", UriKind.Relative));
Uri partUriDocument = PackUriHelper.CreatePartUri(
new Uri(documentPath, UriKind.Relative));
Uri partUriResource = PackUriHelper.CreatePartUri(
new Uri(resourcePath, UriKind.Relative));
// Create the Package
// (If the package file already exists, FileMode.Create will
// automatically delete it first before creating a new one.
// The 'using' statement insures that 'package' is
// closed and disposed when it goes out of scope.)
using (Package package =
Package.Open(packagePath, FileMode.Create))
{
// Add the Document part to the Package
PackagePart packagePartDocument =
package.CreatePart(partUriDocument,
System.Net.Mime.MediaTypeNames.Text.Xml);
// Copy the data to the Document Part
using (FileStream fileStream = new FileStream(
documentPath, FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePartDocument.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
// Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri,
TargetMode.Internal,
PackageRelationshipType);
// Add a Resource Part to the Package
PackagePart packagePartResource =
package.CreatePart(partUriResource,
System.Net.Mime.MediaTypeNames.Image.Jpeg);
// Copy the data to the Resource Part
using (FileStream fileStream = new FileStream(
resourcePath, FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePartResource.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
// Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(
new Uri(@"../resources/image1.jpg",
UriKind.Relative),
TargetMode.Internal,
ResourceRelationshipType);
}// end:using (Package package) - Close and dispose package.
}// end:CreatePackage()
// --------------------------- CopyStream ---------------------------
/// <summary>
/// Copies data from a source stream to a target stream.</summary>
/// <param name="source">
/// The source stream to copy from.</param>
/// <param name="target">
/// The destination stream to copy to.</param>
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}// end:CopyStream()
' -------------------------- CreatePackage --------------------------
''' <summary>
''' Creates a package zip file containing specified
''' content and resource files.</summary>
Private Shared Sub CreatePackage()
' Convert system path and file names to Part URIs. In this example
' Dim partUriDocument as Uri /* /Content/Document.xml */ =
' PackUriHelper.CreatePartUri(
' New Uri("Content\Document.xml", UriKind.Relative))
' Dim partUriResource as Uri /* /Resources/Image1.jpg */ =
' PackUriHelper.CreatePartUri(
' New Uri("Resources\Image1.jpg", UriKind.Relative))
Dim partUriDocument As Uri = PackUriHelper.CreatePartUri(New Uri(documentPath, UriKind.Relative))
Dim partUriResource As Uri = PackUriHelper.CreatePartUri(New Uri(resourcePath, UriKind.Relative))
' Create the Package
' (If the package file already exists, FileMode.Create will
' automatically delete it first before creating a new one.
' The 'using' statement insures that 'package' is
' closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Create)
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)
' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)
' Add a Resource Part to the Package
Dim packagePartResource As PackagePart = package.CreatePart(partUriResource, System.Net.Mime.MediaTypeNames.Image.Jpeg)
' Copy the data to the Resource Part
Using fileStream As New FileStream(resourcePath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartResource.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(New Uri("../resources/image1.jpg", UriKind.Relative), TargetMode.Internal, ResourceRelationshipType)
End Using ' end:using (Package package) - Close and dispose package.
End Sub
' --------------------------- CopyStream ---------------------------
''' <summary>
''' Copies data from a source stream to a target stream.</summary>
''' <param name="source">
''' The source stream to copy from.</param>
''' <param name="target">
''' The destination stream to copy to.</param>
Private Shared Sub CopyStream(ByVal source As Stream, ByVal target As Stream)
Const bufSize As Integer = &H1000
Dim buf(bufSize - 1) As Byte
Dim bytesRead As Integer = 0
bytesRead = source.Read(buf, 0, bufSize)
Do While bytesRead > 0
target.Write(buf, 0, bytesRead)
bytesRead = source.Read(buf, 0, bufSize)
Loop
End Sub
Commenti
ZipPackage è il tipo di pacchetto predefinito utilizzato dal Open metodo .
Per altre informazioni, vedere la specifica OPC (Open Packaging Conventions) disponibile per il download all'indirizzo https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.
Open(Stream)
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
Apre un pacchetto su un flusso di IO specificato.
public:
static System::IO::Packaging::Package ^ Open(System::IO::Stream ^ stream);
public static System.IO.Packaging.Package Open (System.IO.Stream stream);
static member Open : System.IO.Stream -> System.IO.Packaging.Package
Public Shared Function Open (stream As Stream) As Package
Parametri
- stream
- Stream
Il flusso di IO sul quale aprire il pacchetto.
Restituisce
Il pacchetto aperto.
Eccezioni
stream
è null
.
Il pacchetto da aprire richiede autorizzazione di lettura o di lettura e scrittura e l'oggetto stream
specificato è di sola scrittura; oppure il pacchetto da aprire richiede autorizzazione di scrittura o di lettura e scrittura e l'oggetto stream
specificato è di sola lettura.
Commenti
ZipPackage è il tipo di pacchetto predefinito utilizzato dal Open metodo .
Per altre informazioni, vedere la specifica OPC (Open Packaging Conventions) disponibile per il download all'indirizzo https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.
Si applica a
Open(String)
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
Apre un pacchetto in un percorso e con nome file specificati.
public:
static System::IO::Packaging::Package ^ Open(System::String ^ path);
public static System.IO.Packaging.Package Open (string path);
static member Open : string -> System.IO.Packaging.Package
Public Shared Function Open (path As String) As Package
Parametri
- path
- String
Il nome e il percorso del file del pacchetto.
Restituisce
Il pacchetto aperto.
Eccezioni
path
è null
.
Esempio
Nell'esempio seguente viene illustrato come creare un nuovo Package oggetto che incorpora PackageRelationship e PackagePart gli elementi insieme ai dati archiviati.
// -------------------------- CreatePackage --------------------------
/// <summary>
/// Creates a package zip file containing specified
/// content and resource files.</summary>
private static void CreatePackage()
{
// Convert system path and file names to Part URIs. In this example
// Uri partUriDocument /* /Content/Document.xml */ =
// PackUriHelper.CreatePartUri(
// new Uri("Content\Document.xml", UriKind.Relative));
// Uri partUriResource /* /Resources/Image1.jpg */ =
// PackUriHelper.CreatePartUri(
// new Uri("Resources\Image1.jpg", UriKind.Relative));
Uri partUriDocument = PackUriHelper.CreatePartUri(
new Uri(documentPath, UriKind.Relative));
Uri partUriResource = PackUriHelper.CreatePartUri(
new Uri(resourcePath, UriKind.Relative));
// Create the Package
// (If the package file already exists, FileMode.Create will
// automatically delete it first before creating a new one.
// The 'using' statement insures that 'package' is
// closed and disposed when it goes out of scope.)
using (Package package =
Package.Open(packagePath, FileMode.Create))
{
// Add the Document part to the Package
PackagePart packagePartDocument =
package.CreatePart(partUriDocument,
System.Net.Mime.MediaTypeNames.Text.Xml);
// Copy the data to the Document Part
using (FileStream fileStream = new FileStream(
documentPath, FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePartDocument.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
// Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri,
TargetMode.Internal,
PackageRelationshipType);
// Add a Resource Part to the Package
PackagePart packagePartResource =
package.CreatePart(partUriResource,
System.Net.Mime.MediaTypeNames.Image.Jpeg);
// Copy the data to the Resource Part
using (FileStream fileStream = new FileStream(
resourcePath, FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePartResource.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
// Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(
new Uri(@"../resources/image1.jpg",
UriKind.Relative),
TargetMode.Internal,
ResourceRelationshipType);
}// end:using (Package package) - Close and dispose package.
}// end:CreatePackage()
// --------------------------- CopyStream ---------------------------
/// <summary>
/// Copies data from a source stream to a target stream.</summary>
/// <param name="source">
/// The source stream to copy from.</param>
/// <param name="target">
/// The destination stream to copy to.</param>
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}// end:CopyStream()
' -------------------------- CreatePackage --------------------------
''' <summary>
''' Creates a package zip file containing specified
''' content and resource files.</summary>
Private Shared Sub CreatePackage()
' Convert system path and file names to Part URIs. In this example
' Dim partUriDocument as Uri /* /Content/Document.xml */ =
' PackUriHelper.CreatePartUri(
' New Uri("Content\Document.xml", UriKind.Relative))
' Dim partUriResource as Uri /* /Resources/Image1.jpg */ =
' PackUriHelper.CreatePartUri(
' New Uri("Resources\Image1.jpg", UriKind.Relative))
Dim partUriDocument As Uri = PackUriHelper.CreatePartUri(New Uri(documentPath, UriKind.Relative))
Dim partUriResource As Uri = PackUriHelper.CreatePartUri(New Uri(resourcePath, UriKind.Relative))
' Create the Package
' (If the package file already exists, FileMode.Create will
' automatically delete it first before creating a new one.
' The 'using' statement insures that 'package' is
' closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Create)
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)
' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)
' Add a Resource Part to the Package
Dim packagePartResource As PackagePart = package.CreatePart(partUriResource, System.Net.Mime.MediaTypeNames.Image.Jpeg)
' Copy the data to the Resource Part
Using fileStream As New FileStream(resourcePath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartResource.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(New Uri("../resources/image1.jpg", UriKind.Relative), TargetMode.Internal, ResourceRelationshipType)
End Using ' end:using (Package package) - Close and dispose package.
End Sub
' --------------------------- CopyStream ---------------------------
''' <summary>
''' Copies data from a source stream to a target stream.</summary>
''' <param name="source">
''' The source stream to copy from.</param>
''' <param name="target">
''' The destination stream to copy to.</param>
Private Shared Sub CopyStream(ByVal source As Stream, ByVal target As Stream)
Const bufSize As Integer = &H1000
Dim buf(bufSize - 1) As Byte
Dim bytesRead As Integer = 0
bytesRead = source.Read(buf, 0, bufSize)
Do While bytesRead > 0
target.Write(buf, 0, bytesRead)
bytesRead = source.Read(buf, 0, bufSize)
Loop
End Sub
Commenti
ZipPackage è il tipo di pacchetto predefinito utilizzato dal Open metodo .
Questo Open metodo apre il pacchetto con attributi OpenOrCreatepredefiniti , ReadWritee None (per specificare attributi diversi usa uno degli altri overload del metodo Open).
Per altre informazioni, vedere la specifica OPC (Open Packaging Conventions) disponibile per il download all'indirizzo https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.
Si applica a
Open(Stream, FileMode)
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
Apre un pacchetto con un flusso di IO e modalità file specificati.
public:
static System::IO::Packaging::Package ^ Open(System::IO::Stream ^ stream, System::IO::FileMode packageMode);
public static System.IO.Packaging.Package Open (System.IO.Stream stream, System.IO.FileMode packageMode);
static member Open : System.IO.Stream * System.IO.FileMode -> System.IO.Packaging.Package
Public Shared Function Open (stream As Stream, packageMode As FileMode) As Package
Parametri
- stream
- Stream
Il flusso di IO sul quale aprire il pacchetto.
- packageMode
- FileMode
La modalità file nella quale aprire il pacchetto.
Restituisce
Il pacchetto aperto.
Eccezioni
stream
è null
.
Il valore packageMode
non è valido.
Il pacchetto da aprire richiede autorizzazione di lettura o di lettura e scrittura e l'oggetto stream
specificato è di sola scrittura; oppure il pacchetto da aprire richiede autorizzazione di scrittura o di lettura e scrittura e l'oggetto stream
specificato è di sola lettura.
Commenti
ZipPackage è il tipo di pacchetto predefinito utilizzato dal Open metodo .
Per altre informazioni, vedere la specifica OPC (Open Packaging Conventions) disponibile per il download all'indirizzo https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.
Si applica a
Open(String, FileMode)
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
Apre un pacchetto in un percorso specificato utilizzando una modalità di file specificata.
public:
static System::IO::Packaging::Package ^ Open(System::String ^ path, System::IO::FileMode packageMode);
public static System.IO.Packaging.Package Open (string path, System.IO.FileMode packageMode);
static member Open : string * System.IO.FileMode -> System.IO.Packaging.Package
Public Shared Function Open (path As String, packageMode As FileMode) As Package
Parametri
- path
- String
Il nome e il percorso del file del pacchetto.
- packageMode
- FileMode
La modalità file nella quale aprire il pacchetto.
Restituisce
Il pacchetto aperto.
Eccezioni
path
è null
.
Il valore di packageMode
non è valido.
Esempio
Nell'esempio seguente viene illustrato come creare un nuovo Package oggetto che incorpora PackageRelationship e PackagePart gli elementi insieme ai dati archiviati.
// -------------------------- CreatePackage --------------------------
/// <summary>
/// Creates a package zip file containing specified
/// content and resource files.</summary>
private static void CreatePackage()
{
// Convert system path and file names to Part URIs. In this example
// Uri partUriDocument /* /Content/Document.xml */ =
// PackUriHelper.CreatePartUri(
// new Uri("Content\Document.xml", UriKind.Relative));
// Uri partUriResource /* /Resources/Image1.jpg */ =
// PackUriHelper.CreatePartUri(
// new Uri("Resources\Image1.jpg", UriKind.Relative));
Uri partUriDocument = PackUriHelper.CreatePartUri(
new Uri(documentPath, UriKind.Relative));
Uri partUriResource = PackUriHelper.CreatePartUri(
new Uri(resourcePath, UriKind.Relative));
// Create the Package
// (If the package file already exists, FileMode.Create will
// automatically delete it first before creating a new one.
// The 'using' statement insures that 'package' is
// closed and disposed when it goes out of scope.)
using (Package package =
Package.Open(packagePath, FileMode.Create))
{
// Add the Document part to the Package
PackagePart packagePartDocument =
package.CreatePart(partUriDocument,
System.Net.Mime.MediaTypeNames.Text.Xml);
// Copy the data to the Document Part
using (FileStream fileStream = new FileStream(
documentPath, FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePartDocument.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
// Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri,
TargetMode.Internal,
PackageRelationshipType);
// Add a Resource Part to the Package
PackagePart packagePartResource =
package.CreatePart(partUriResource,
System.Net.Mime.MediaTypeNames.Image.Jpeg);
// Copy the data to the Resource Part
using (FileStream fileStream = new FileStream(
resourcePath, FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePartResource.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
// Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(
new Uri(@"../resources/image1.jpg",
UriKind.Relative),
TargetMode.Internal,
ResourceRelationshipType);
}// end:using (Package package) - Close and dispose package.
}// end:CreatePackage()
// --------------------------- CopyStream ---------------------------
/// <summary>
/// Copies data from a source stream to a target stream.</summary>
/// <param name="source">
/// The source stream to copy from.</param>
/// <param name="target">
/// The destination stream to copy to.</param>
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}// end:CopyStream()
' -------------------------- CreatePackage --------------------------
''' <summary>
''' Creates a package zip file containing specified
''' content and resource files.</summary>
Private Shared Sub CreatePackage()
' Convert system path and file names to Part URIs. In this example
' Dim partUriDocument as Uri /* /Content/Document.xml */ =
' PackUriHelper.CreatePartUri(
' New Uri("Content\Document.xml", UriKind.Relative))
' Dim partUriResource as Uri /* /Resources/Image1.jpg */ =
' PackUriHelper.CreatePartUri(
' New Uri("Resources\Image1.jpg", UriKind.Relative))
Dim partUriDocument As Uri = PackUriHelper.CreatePartUri(New Uri(documentPath, UriKind.Relative))
Dim partUriResource As Uri = PackUriHelper.CreatePartUri(New Uri(resourcePath, UriKind.Relative))
' Create the Package
' (If the package file already exists, FileMode.Create will
' automatically delete it first before creating a new one.
' The 'using' statement insures that 'package' is
' closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Create)
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)
' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)
' Add a Resource Part to the Package
Dim packagePartResource As PackagePart = package.CreatePart(partUriResource, System.Net.Mime.MediaTypeNames.Image.Jpeg)
' Copy the data to the Resource Part
Using fileStream As New FileStream(resourcePath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartResource.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(New Uri("../resources/image1.jpg", UriKind.Relative), TargetMode.Internal, ResourceRelationshipType)
End Using ' end:using (Package package) - Close and dispose package.
End Sub
' --------------------------- CopyStream ---------------------------
''' <summary>
''' Copies data from a source stream to a target stream.</summary>
''' <param name="source">
''' The source stream to copy from.</param>
''' <param name="target">
''' The destination stream to copy to.</param>
Private Shared Sub CopyStream(ByVal source As Stream, ByVal target As Stream)
Const bufSize As Integer = &H1000
Dim buf(bufSize - 1) As Byte
Dim bytesRead As Integer = 0
bytesRead = source.Read(buf, 0, bufSize)
Do While bytesRead > 0
target.Write(buf, 0, bytesRead)
bytesRead = source.Read(buf, 0, bufSize)
Loop
End Sub
Commenti
ZipPackage è il tipo di pacchetto predefinito utilizzato dal Open metodo .
Questo Open metodo apre il pacchetto con attributi ReadWrite predefiniti e None per specificare attributi diversi utilizzano uno degli altri overload del metodo Open.
Per altre informazioni, vedere la specifica OPC (Open Packaging Conventions) disponibile per il download all'indirizzo https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.
Si applica a
Open(Stream, FileMode, FileAccess)
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
Apre un pacchetto con un'impostazione specificata di flusso di IO, modalità file e accesso ai file.
public:
static System::IO::Packaging::Package ^ Open(System::IO::Stream ^ stream, System::IO::FileMode packageMode, System::IO::FileAccess packageAccess);
public static System.IO.Packaging.Package Open (System.IO.Stream stream, System.IO.FileMode packageMode, System.IO.FileAccess packageAccess);
static member Open : System.IO.Stream * System.IO.FileMode * System.IO.FileAccess -> System.IO.Packaging.Package
Public Shared Function Open (stream As Stream, packageMode As FileMode, packageAccess As FileAccess) As Package
Parametri
- stream
- Stream
Il flusso di IO sul quale aprire il pacchetto.
- packageMode
- FileMode
La modalità file nella quale aprire il pacchetto.
- packageAccess
- FileAccess
L'accesso ai file con il quale aprire il pacchetto.
Restituisce
Il pacchetto aperto.
Eccezioni
stream
è null
.
Il valore di packageMode
o packageAccess
non è valido.
Il pacchetto da aprire richiede autorizzazione di lettura o di lettura e scrittura e l'oggetto stream
specificato è di sola scrittura; oppure il pacchetto da aprire richiede autorizzazione di scrittura o di lettura e scrittura e l'oggetto stream
specificato è di sola lettura.
Commenti
ZipPackage è il tipo di pacchetto predefinito utilizzato dal Open metodo .
Per altre informazioni, vedere la specifica OPC (Open Packaging Conventions) disponibile per il download all'indirizzo https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.
Si applica a
Open(String, FileMode, FileAccess)
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
Apre un pacchetto in un percorso specificato utilizzando un'impostazione specificato di modalità file e accesso ai file.
public:
static System::IO::Packaging::Package ^ Open(System::String ^ path, System::IO::FileMode packageMode, System::IO::FileAccess packageAccess);
public static System.IO.Packaging.Package Open (string path, System.IO.FileMode packageMode, System.IO.FileAccess packageAccess);
static member Open : string * System.IO.FileMode * System.IO.FileAccess -> System.IO.Packaging.Package
Public Shared Function Open (path As String, packageMode As FileMode, packageAccess As FileAccess) As Package
Parametri
- path
- String
Il nome e il percorso del file del pacchetto.
- packageMode
- FileMode
La modalità file nella quale aprire il pacchetto.
- packageAccess
- FileAccess
L'accesso ai file con il quale aprire il pacchetto.
Restituisce
Il pacchetto aperto.
Eccezioni
path
è null
.
Il valore di packageMode
o packageAccess
non è valido.
Esempio
Nell'esempio seguente viene illustrato come aprire e leggere un Package oggetto contenente PackageRelationship gli elementi e PackagePart insieme ai dati archiviati.
// Open the Package.
// ('using' statement insures that 'package' is
// closed and disposed when it goes out of scope.)
using (Package package =
Package.Open(packagePath, FileMode.Open, FileAccess.Read))
{
PackagePart documentPart = null;
PackagePart resourcePart = null;
// Get the Package Relationships and look for
// the Document part based on the RelationshipType
Uri uriDocumentTarget = null;
foreach (PackageRelationship relationship in
package.GetRelationshipsByType(PackageRelationshipType))
{
// Resolve the Relationship Target Uri
// so the Document Part can be retrieved.
uriDocumentTarget = PackUriHelper.ResolvePartUri(
new Uri("/", UriKind.Relative), relationship.TargetUri);
// Open the Document Part, write the contents to a file.
documentPart = package.GetPart(uriDocumentTarget);
ExtractPart(documentPart, targetDirectory);
}
// Get the Document part's Relationships,
// and look for required resources.
Uri uriResourceTarget = null;
foreach (PackageRelationship relationship in
documentPart.GetRelationshipsByType(
ResourceRelationshipType))
{
// Resolve the Relationship Target Uri
// so the Resource Part can be retrieved.
uriResourceTarget = PackUriHelper.ResolvePartUri(
documentPart.Uri, relationship.TargetUri);
// Open the Resource Part and write the contents to a file.
resourcePart = package.GetPart(uriResourceTarget);
ExtractPart(resourcePart, targetDirectory);
}
}// end:using(Package package) - Close & dispose package.
' Open the Package.
' ('using' statement insures that 'package' is
' closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Open, FileAccess.Read)
Dim documentPart As PackagePart = Nothing
Dim resourcePart As PackagePart = Nothing
' Get the Package Relationships and look for
' the Document part based on the RelationshipType
Dim uriDocumentTarget As Uri = Nothing
For Each relationship As PackageRelationship In package.GetRelationshipsByType(PackageRelationshipType)
' Resolve the Relationship Target Uri
' so the Document Part can be retrieved.
uriDocumentTarget = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), relationship.TargetUri)
' Open the Document Part, write the contents to a file.
documentPart = package.GetPart(uriDocumentTarget)
ExtractPart(documentPart, targetDirectory)
Next relationship
' Get the Document part's Relationships,
' and look for required resources.
Dim uriResourceTarget As Uri = Nothing
For Each relationship As PackageRelationship In documentPart.GetRelationshipsByType(ResourceRelationshipType)
' Resolve the Relationship Target Uri
' so the Resource Part can be retrieved.
uriResourceTarget = PackUriHelper.ResolvePartUri(documentPart.Uri, relationship.TargetUri)
' Open the Resource Part and write the contents to a file.
resourcePart = package.GetPart(uriResourceTarget)
ExtractPart(resourcePart, targetDirectory)
Next relationship
End Using ' end:using(Package package) - Close & dispose package.
Commenti
ZipPackage è il tipo di pacchetto predefinito utilizzato dal Open metodo .
Questo Open metodo apre il pacchetto con un attributo predefinito None (per specificare un attributo diverso, usare l'overload del Open metodo).
Per altre informazioni, vedere la specifica OPC (Open Packaging Conventions) disponibile per il download all'indirizzo https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.
Si applica a
Open(String, FileMode, FileAccess, FileShare)
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
- Origine:
- Package.cs
Apre un pacchetto in un percorso specificato utilizzando un'impostazione specificata di modalità file, accesso ai file e condivisione file.
public:
static System::IO::Packaging::Package ^ Open(System::String ^ path, System::IO::FileMode packageMode, System::IO::FileAccess packageAccess, System::IO::FileShare packageShare);
public static System.IO.Packaging.Package Open (string path, System.IO.FileMode packageMode, System.IO.FileAccess packageAccess, System.IO.FileShare packageShare);
static member Open : string * System.IO.FileMode * System.IO.FileAccess * System.IO.FileShare -> System.IO.Packaging.Package
Public Shared Function Open (path As String, packageMode As FileMode, packageAccess As FileAccess, packageShare As FileShare) As Package
Parametri
- path
- String
Il nome e il percorso del file del pacchetto.
- packageMode
- FileMode
La modalità file nella quale aprire il pacchetto.
- packageAccess
- FileAccess
L'accesso ai file con il quale aprire il pacchetto.
- packageShare
- FileShare
La modalità di condivisione file nella quale aprire il pacchetto.
Restituisce
Il pacchetto aperto.
Eccezioni
path
è null
.
Il valore di packageMode
, packageAccess
, o packageShare
non è valido.
Esempio
Nell'esempio seguente viene illustrato come creare un nuovo Package oggetto che incorpora PackageRelationship e PackagePart gli elementi insieme ai dati archiviati.
// -------------------------- CreatePackage --------------------------
/// <summary>
/// Creates a package zip file containing specified
/// content and resource files.</summary>
private static void CreatePackage()
{
// Convert system path and file names to Part URIs. In this example
// Uri partUriDocument /* /Content/Document.xml */ =
// PackUriHelper.CreatePartUri(
// new Uri("Content\Document.xml", UriKind.Relative));
// Uri partUriResource /* /Resources/Image1.jpg */ =
// PackUriHelper.CreatePartUri(
// new Uri("Resources\Image1.jpg", UriKind.Relative));
Uri partUriDocument = PackUriHelper.CreatePartUri(
new Uri(documentPath, UriKind.Relative));
Uri partUriResource = PackUriHelper.CreatePartUri(
new Uri(resourcePath, UriKind.Relative));
// Create the Package
// (If the package file already exists, FileMode.Create will
// automatically delete it first before creating a new one.
// The 'using' statement insures that 'package' is
// closed and disposed when it goes out of scope.)
using (Package package =
Package.Open(packagePath, FileMode.Create))
{
// Add the Document part to the Package
PackagePart packagePartDocument =
package.CreatePart(partUriDocument,
System.Net.Mime.MediaTypeNames.Text.Xml);
// Copy the data to the Document Part
using (FileStream fileStream = new FileStream(
documentPath, FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePartDocument.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
// Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri,
TargetMode.Internal,
PackageRelationshipType);
// Add a Resource Part to the Package
PackagePart packagePartResource =
package.CreatePart(partUriResource,
System.Net.Mime.MediaTypeNames.Image.Jpeg);
// Copy the data to the Resource Part
using (FileStream fileStream = new FileStream(
resourcePath, FileMode.Open, FileAccess.Read))
{
CopyStream(fileStream, packagePartResource.GetStream());
}// end:using(fileStream) - Close and dispose fileStream.
// Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(
new Uri(@"../resources/image1.jpg",
UriKind.Relative),
TargetMode.Internal,
ResourceRelationshipType);
}// end:using (Package package) - Close and dispose package.
}// end:CreatePackage()
// --------------------------- CopyStream ---------------------------
/// <summary>
/// Copies data from a source stream to a target stream.</summary>
/// <param name="source">
/// The source stream to copy from.</param>
/// <param name="target">
/// The destination stream to copy to.</param>
private static void CopyStream(Stream source, Stream target)
{
const int bufSize = 0x1000;
byte[] buf = new byte[bufSize];
int bytesRead = 0;
while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
target.Write(buf, 0, bytesRead);
}// end:CopyStream()
' -------------------------- CreatePackage --------------------------
''' <summary>
''' Creates a package zip file containing specified
''' content and resource files.</summary>
Private Shared Sub CreatePackage()
' Convert system path and file names to Part URIs. In this example
' Dim partUriDocument as Uri /* /Content/Document.xml */ =
' PackUriHelper.CreatePartUri(
' New Uri("Content\Document.xml", UriKind.Relative))
' Dim partUriResource as Uri /* /Resources/Image1.jpg */ =
' PackUriHelper.CreatePartUri(
' New Uri("Resources\Image1.jpg", UriKind.Relative))
Dim partUriDocument As Uri = PackUriHelper.CreatePartUri(New Uri(documentPath, UriKind.Relative))
Dim partUriResource As Uri = PackUriHelper.CreatePartUri(New Uri(resourcePath, UriKind.Relative))
' Create the Package
' (If the package file already exists, FileMode.Create will
' automatically delete it first before creating a new one.
' The 'using' statement insures that 'package' is
' closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Create)
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)
' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)
' Add a Resource Part to the Package
Dim packagePartResource As PackagePart = package.CreatePart(partUriResource, System.Net.Mime.MediaTypeNames.Image.Jpeg)
' Copy the data to the Resource Part
Using fileStream As New FileStream(resourcePath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartResource.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(New Uri("../resources/image1.jpg", UriKind.Relative), TargetMode.Internal, ResourceRelationshipType)
End Using ' end:using (Package package) - Close and dispose package.
End Sub
' --------------------------- CopyStream ---------------------------
''' <summary>
''' Copies data from a source stream to a target stream.</summary>
''' <param name="source">
''' The source stream to copy from.</param>
''' <param name="target">
''' The destination stream to copy to.</param>
Private Shared Sub CopyStream(ByVal source As Stream, ByVal target As Stream)
Const bufSize As Integer = &H1000
Dim buf(bufSize - 1) As Byte
Dim bytesRead As Integer = 0
bytesRead = source.Read(buf, 0, bufSize)
Do While bytesRead > 0
target.Write(buf, 0, bytesRead)
bytesRead = source.Read(buf, 0, bufSize)
Loop
End Sub
Commenti
ZipPackage è il tipo di pacchetto predefinito utilizzato dal Open metodo .
Per altre informazioni, vedere la specifica OPC (Open Packaging Conventions) disponibile per il download all'indirizzo https://www.ecma-international.org/publications-and-standards/standards/ecma-376/.