System.IO.Packaging.Package.DeletePart C# (CSharp) Méthode

DeletePart() public méthode

public DeletePart ( Uri partUri ) : void
partUri System.Uri
Résultat void
        public void DeletePart(Uri partUri)
        {
            CheckIsReadOnly();
            Check.PartUri(partUri);

            PackagePart part = GetPart(partUri);
            if (part != null)
            {
                if (part.Package == null)
                    throw new InvalidOperationException("This part has already been removed");

                // FIXME: MS.NET doesn't remove the relationship part
                // Instead it throws an exception if you try to use it
                if (PartExists(part.RelationshipsPartUri))
                    GetPart(part.RelationshipsPartUri).Package = null;

                part.Package = null;
                DeletePartCore(partUri);
                PartsCollection.Parts.RemoveAll(p => p.Uri == partUri);
            }
        }

Usage Example

Exemple #1
0
        /// <summary>
        /// Creates a new instance of the ExcelPackage class based on a existing file or creates a new file. 
        /// </summary>
        /// <param name="newFile">If newFile exists, it is opened.  Otherwise it is created from scratch.</param>
        private ExcelPackage(FileInfo newFile)
        {
            _outputFolderPath = newFile.DirectoryName;
            if (newFile.Exists)
            {
                // open the existing package
                _package = Package.Open(newFile.FullName, FileMode.Open, FileAccess.ReadWrite);
            }
            else
            {
                // create a new package and add the main workbook.xml part
                _package = Package.Open(newFile.FullName, FileMode.Create, FileAccess.ReadWrite);

                // save a temporary part to create the default application/xml content type
                var uriDefaultContentType = new Uri("/default.xml", UriKind.Relative);
                var partTemp = _package.CreatePart(uriDefaultContentType, "application/xml");

                var workbook = Workbook.WorkbookXml; // this will create the workbook xml in the package

                // create the relationship to the main part
                _package.CreateRelationship(Workbook.WorkbookUri,
                                            TargetMode.Internal,
                                            schemaRelationships + "/officeDocument");

                // remove the temporary part that created the default xml content type
                _package.DeletePart(uriDefaultContentType);
            }
        }
All Usage Examples Of System.IO.Packaging.Package::DeletePart