本文整理汇总了C#中System.IO.Packaging.Package.DeletePart方法的典型用法代码示例。如果您正苦于以下问题:C# Package.DeletePart方法的具体用法?C# Package.DeletePart怎么用?C# Package.DeletePart使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Packaging.Package
的用法示例。
在下文中一共展示了Package.DeletePart方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExcelPackage
/// <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);
}
}
示例2: AddFileToPackage
public static void AddFileToPackage(Package package, string uri, string filePath, string contentType)
{
FileInfo info = new FileInfo(filePath);
uri = MakeUriSafe(uri);
if (!info.Exists)
{
throw new FileNotFoundException(Strings.Package_FileCouldNotBeAdded, filePath);
}
Uri partUri = new Uri(uri, UriKind.Relative);
string extension = info.Extension;
if (package.PartExists(partUri))
{
package.DeletePart(partUri);
}
PackagePart part = package.CreatePart(partUri, contentType, CompressionOption.Maximum);
using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (Stream stream2 = part.GetStream())
{
CopyStream(stream, stream2);
}
stream.Close();
}
}
示例3: RenamePath
public static void RenamePath(Package package, string oldUri, string newUri)
{
Uri partUri = new Uri(oldUri, UriKind.Relative);
Uri uri2 = new Uri(newUri, UriKind.Relative);
if (package.PartExists(partUri))
{
PackagePart part = package.GetPart(partUri);
PackagePart part2 = package.CreatePart(uri2, part.ContentType, part.CompressionOption);
using (Stream stream = part.GetStream())
{
using (Stream stream2 = part2.GetStream())
{
CopyStream(stream, stream2);
}
}
package.DeletePart(partUri);
}
}
示例4: RemoveFileFromPackage
public static void RemoveFileFromPackage(Package package, string uri)
{
Uri partUri = new Uri(uri, UriKind.Relative);
if (package.PartExists(partUri))
{
package.DeletePart(partUri);
}
}
示例5: AddPart
static void AddPart(Package zip, string root, string path) {
Uri uri = PackUriHelper.CreatePartUri(new Uri(path, UriKind.Relative));
if (zip.PartExists(uri)) {
zip.DeletePart(uri);
}
PackagePart part = zip.CreatePart(uri, "", CompressionOption.Normal);
using (FileStream fileStream = new FileStream(Path.Combine(root, path), FileMode.Open, FileAccess.Read)) {
using (Stream dest = part.GetStream()) {
CopyStream(fileStream, dest);
}
}
}
示例6: StmCreatePart
/* S T M C R E A T E P A R T */
/*----------------------------------------------------------------------------
%%Function: StmCreatePart
%%Qualified: ArbWeb.OOXML.StmCreatePart
%%Contact: rlittle
----------------------------------------------------------------------------*/
public static Stream StmCreatePart(Package pkg, string sUri, string sContentType, out PackagePart prt)
{
Uri uriTeams = new System.Uri(sUri, UriKind.Relative);
prt = pkg.GetPart(uriTeams);
List<PackageRelationship> plrel = new List<PackageRelationship>();
foreach (PackageRelationship rel in prt.GetRelationships())
{
plrel.Add(rel);
}
prt = null;
pkg.DeletePart(uriTeams);
prt = pkg.CreatePart(uriTeams, sContentType);
foreach (PackageRelationship rel in plrel)
{
prt.CreateRelationship(rel.TargetUri, rel.TargetMode, rel.RelationshipType, rel.Id);
}
return prt.GetStream(FileMode.Create, FileAccess.Write);
}
示例7: AddToArchive
private static void AddToArchive(Package zip, string fileToAdd, string root)
{
try
{
string uriFileName = fileToAdd.Replace(" ", "_");
FileAttributes attr = System.IO.File.GetAttributes(fileToAdd);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
root = root + "/" + Path.GetFileName(uriFileName);
string[] subfolders = Directory.GetDirectories(fileToAdd);
for (int i = 0; i < subfolders.Length; i++)
{
AddToArchive(zip, subfolders[i], root);
}
string[] subfiles = Directory.GetFiles(fileToAdd);
for (int i = 0; i < subfiles.Length; i++)
{
AddToArchive(zip, subfiles[i], root);
}
}
else
{
string zipUri = string.Concat(root + "/", Path.GetFileName(uriFileName));
Uri partUri = new Uri(zipUri, UriKind.Relative);
string contentType = System.Net.Mime.MediaTypeNames.Application.Zip;
if (zip.PartExists(partUri)) zip.DeletePart(partUri);
PackagePart pkgPart = zip.CreatePart(partUri, contentType, CompressionOption.Normal);
Byte[] bytes = System.IO.File.ReadAllBytes(fileToAdd);
pkgPart.GetStream().Write(bytes, 0, bytes.Length);
}
}
catch (Exception ex)
{
TextWindow.WriteLine(fileToAdd);
Utilities.OnError(Utilities.GetCurrentMethod(), ex);
}
}
示例8: AddToArchive
private bool AddToArchive(Package zip, string fileToAdd,Backup bck)
{
// Replace spaces with an underscore (_)
string uriFileName = fileToAdd.Replace(" ", "_");
// A Uri always starts with a forward slash "/"
string zipUri = string.Concat("/", Path.GetFileName(uriFileName));
Uri partUri = new Uri(zipUri, UriKind.Relative);
string contentType = MediaTypeNames.Application.Zip;
if (!zip.PartExists(partUri))
{
zip.DeletePart(partUri);
}
if (!zip.PartExists(partUri))
{
PackagePart pkgPart = zip.CreatePart(partUri, contentType, ConvertCompIntoCompressOption(bck.compressLevel));
// Read all of the bytes from the file to add to the zip file
Byte[] bites = null;
if (bck.isVss)
{
MessageBox.Show(sShadowPath + fileToAdd);
//bites = wApi.GetFileData(sShadowPath + Path.GetFileName(fileToAdd));
}
else
{
try
{
bites = File.ReadAllBytes(fileToAdd);
}
catch (Exception e)
{
bites = null;
}
}
//Compress and write the bytes to the zip file
if (bites != null)
{
pkgPart.GetStream().Write(bites, 0, bites.Length);
return true;
}
else
return false;
}
else
return false;
}