本文整理汇总了C#中System.IO.Packaging.PackagePart类的典型用法代码示例。如果您正苦于以下问题:C# PackagePart类的具体用法?C# PackagePart怎么用?C# PackagePart使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
PackagePart类属于System.IO.Packaging命名空间,在下文中一共展示了PackagePart类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExtractContentFile
private static void ExtractContentFile(string rootFolder, PackagePart contentFile)
{
// Initially create file under the folder specified
string contentFilePath = contentFile.Uri.OriginalString.Replace('/', Path.DirectorySeparatorChar);
if (contentFilePath.StartsWith(Path.DirectorySeparatorChar.ToString(CultureInfo.InvariantCulture)))
{
contentFilePath = contentFilePath.TrimStart(Path.DirectorySeparatorChar);
}
contentFilePath = Path.Combine(rootFolder, contentFilePath);
//Check for the folder already exists. If not then create that folder
if (!Directory.Exists(Path.GetDirectoryName(contentFilePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(contentFilePath));
}
using (var newFileStream = File.Create(contentFilePath))
{
newFileStream.Close();
var content = new byte[contentFile.GetStream().Length];
contentFile.GetStream().Read(content, 0, content.Length);
File.WriteAllBytes(contentFilePath, content);
}
}
示例2: UpdatePackageManifest
private void UpdatePackageManifest(Package package, PackagePart updatedPart)
{
if (package == null)
throw new ArgumentNullException(nameof(package));
if (updatedPart == null)
throw new ArgumentNullException(nameof(updatedPart));
if (package.FileOpenAccess != FileAccess.ReadWrite)
throw new InvalidOperationException("Package must be open for reading and writing");
var manifestRelation = package.GetRelationship("MANIFEST");
var manifestPart = package.GetPart(manifestRelation.TargetUri);
// parse manifest
var manifest = new PackageManifest(manifestPart, null);
// rehash updated part
var csDefPart = manifest.Items.FirstOrDefault(i => i.PartUri == updatedPart.Uri);
if (csDefPart == null)
throw new InvalidOperationException(string.Format("Unable to find part '{0}' in package manifest", updatedPart.Uri));
csDefPart.Hash = manifest.HashAlgorithm.ComputeHash(updatedPart.GetStream(FileMode.Open, FileAccess.Read)); ;
csDefPart.ModifiedDate = DateTime.UtcNow;
var manifestStream = manifestPart.GetStream(FileMode.Open, FileAccess.Write);
manifest.WriteToStream(manifestStream);
}
示例3: SignPackagePart
private void SignPackagePart(PackagePart packagePart)
{
foreach (var signer in _signerRepository)
{
signer.TrySign(packagePart);
}
}
示例4: TrySignPackagePart
private static bool TrySignPackagePart(PackagePart packagePart, string keyPath, string keyPassword, bool signedPackage)
{
if (packagePart.Uri.ToString().EndsWith(".exe")
|| packagePart.Uri.ToString().EndsWith(".dll"))
{
string tempPath = Path.GetTempFileName();
try
{
using (var stream = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write))
{
packagePart.GetStream().CopyTo(stream);
}
if (!SigningHelper.GetAssemblyInfo(tempPath).IsSigned)
{
signedPackage = true;
SigningHelper.SignAssembly(tempPath, keyPath ?? string.Empty, keyPassword ?? string.Empty);
using (var stream = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Read))
{
stream.CopyTo(packagePart.GetStream(FileMode.Create, FileAccess.Write));
}
}
}
finally
{
File.Delete(tempPath);
}
}
return signedPackage;
}
示例5: FromXml
private static IconDef FromXml(XElement xml, PackagePart part)
{
var icon = xml.Attr<string>("icon");
if (icon != null)
icon = part.GetRelationship(icon).TargetUri.OriginalString;
return new IconDef { Icon = icon, Pattern = xml.Attr<string>("pattern")};
}
示例6: UpdateServiceDescriptionPart
protected override void UpdateServiceDescriptionPart(PackagePart serviceDescriptionPart)
{
var definitionSerializer = new XmlSerializer(typeof(ServiceDefinition));
var packageStream = serviceDescriptionPart.GetStream(FileMode.Open, FileAccess.ReadWrite);
var serviceDefinition = definitionSerializer.Deserialize(packageStream) as ServiceDefinition;
UpdateServiceDefinition(serviceDefinition);
// rewind package stream
packageStream.Seek(0, SeekOrigin.Begin);
packageStream.SetLength(0);
var writerSettings = new XmlWriterSettings()
{
Indent = true,
CloseOutput = false
};
// write back to output
using (var writer = XmlWriter.Create(packageStream, writerSettings))
{
definitionSerializer.Serialize(writer, serviceDefinition);
}
}
示例7: GetContentsAsXml
/// <summary>
/// Returns the part contents in xml
/// </summary>
/// <param name="part">System.IO.Packaging.Packagepart</param>
/// <returns></returns>
static XElement GetContentsAsXml(PackagePart part)
{
XNamespace pkg =
"http://schemas.microsoft.com/office/2006/xmlPackage";
if (part.ContentType.EndsWith("xml"))
{
using (Stream partstream = part.GetStream())
using (StreamReader streamReader = new StreamReader(partstream))
{
string streamString = streamReader.ReadToEnd();
XElement newXElement =
new XElement(pkg + "part", new XAttribute(pkg + "name", part.Uri),
new XAttribute(pkg + "contentType", part.ContentType),
new XElement(pkg + "xmlData", XElement.Parse(streamString)));
return newXElement;
}
}
else
{
using (Stream str = part.GetStream())
using (BinaryReader binaryReader = new BinaryReader(str))
{
int len = (int)binaryReader.BaseStream.Length;
byte[] byteArray = binaryReader.ReadBytes(len);
// the following expression creates the base64String, then chunks
// it to lines of 76 characters long
string base64String = (System.Convert.ToBase64String(byteArray))
.Select
(
(c, i) => new
{
Character = c,
Chunk = i / 76
}
)
.GroupBy(c => c.Chunk)
.Aggregate(
new StringBuilder(),
(s, i) =>
s.Append(
i.Aggregate(
new StringBuilder(),
(seed, it) => seed.Append(it.Character),
sb => sb.ToString()
)
)
.Append(Environment.NewLine),
s => s.ToString()
);
return new XElement(pkg + "part",
new XAttribute(pkg + "name", part.Uri),
new XAttribute(pkg + "contentType", part.ContentType),
new XAttribute(pkg + "compression", "store"),
new XElement(pkg + "binaryData", base64String)
);
}
}
}
示例8: LoadAllFromXml
public static List<FontDef> LoadAllFromXml(XElement xml, PackagePart part)
{
if (xml == null) return new List<FontDef>(0);
return xml.Elements("font")
.Select(x => FromXml(x, part))
.ToList();
}
示例9: LoadFromXml
internal static SharedDef LoadFromXml(XElement xml, PackagePart part)
{
if (xml == null) return null;
var res = new SharedDef();
res.LoadBaseXml(xml, part);
return res;
}
示例10: CopyPart
private static void CopyPart(PackagePart oldPart, PackagePart newPart)
{
using (Stream oldPartStream = oldPart.GetStream(),
newPartStream = newPart.GetStream(FileMode.OpenOrCreate))
{
oldPartStream.CopyTo(newPartStream);
}
}
示例11: LoadFromXml
internal static CardDef LoadFromXml(XElement xml, PackagePart part)
{
var backUri = xml.Attr<string>("back");
if (backUri != null) backUri = part.GetRelationship(backUri).TargetUri.OriginalString;
var frontUri = xml.Attr<string>("front");
if (frontUri != null) frontUri = part.GetRelationship(frontUri).TargetUri.OriginalString;
var cd = new CardDef
{
back = backUri,
front = frontUri,
Width = xml.Attr<int>("width"),
Height = xml.Attr<int>("height"),
CornerRadius = xml.Attr<int>("cornerRadius"),
//Properties = xml.Elements(Defs.XmlnsOctgn + "property")
// //.Select(PropertyDef.LoadFromXml)
// .ToDictionary(x => x.Name)
};
var list = xml.Elements(Defs.XmlnsOctgn + "property");
var proplist = new Dictionary<string,PropertyDef>();
foreach (var l in list)
{
var name = l.Attr<string>("name");
string kindStr = l.Attr("textKind", "Free");
PropertyTextKind kind;
switch (kindStr)
{
case "Free":
kind = PropertyTextKind.FreeText;
break;
case "Enum":
kind = PropertyTextKind.Enumeration;
break;
case "Tokens":
kind = PropertyTextKind.Tokens;
break;
default:
throw new InvalidFileFormatException(
string.Format("Unknown value '{0}' for textKind attribute (Property = {1})",
kindStr, name));
}
var prop = new PropertyDef
{
Name = name,
Type = l.Attr<PropertyType>("type"),
IgnoreText = l.Attr<bool>("ignoreText"),
TextKind = kind,
Hidden = l.Attr<bool>("hidden")
};
proplist.Add(name,prop);
}
cd.Properties = proplist;
return cd;
}
示例12: FromXml
public static ScriptDef FromXml(XElement xml, PackagePart part)
{
string srcUri = part.GetRelationship(xml.Attr<string>("src")).TargetUri.OriginalString;
PackagePart scriptPart = part.Package.GetPart(new Uri(srcUri, UriKind.Relative));
using (Stream stream = scriptPart.GetStream(FileMode.Open, FileAccess.Read))
using (var textReader = new StreamReader(stream))
return new ScriptDef {Python = textReader.ReadToEnd(), FileName = srcUri};
}
示例13: LoadFromXml
internal static PlayerDef LoadFromXml(XElement xml, PackagePart part)
{
var res = new PlayerDef
{
IndicatorsFormat = xml.Attr<string>("summary"),
Hand = GroupDef.LoadFromXml(xml.Child("hand"), part, 0)
};
res.LoadBaseXml(xml, part);
return res;
}
示例14: Hyperlink
internal Hyperlink(DocX document, PackagePart mainPart, XElement i)
: base(document, i)
{
this.type = 0;
this.id = i.Attribute(XName.Get("id", DocX.r.NamespaceName)).Value;
StringBuilder sb = new StringBuilder();
HelperFunctions.GetTextRecursive(i, ref sb);
this.text = sb.ToString();
}
示例15: ZipPackageAssemblyReference
public ZipPackageAssemblyReference(PackagePart part)
: base(part)
{
Debug.Assert(Path.StartsWith("lib", StringComparison.OrdinalIgnoreCase), "path doesn't start with lib");
// Get rid of the lib folder
string path = Path.Substring(3).Trim(System.IO.Path.DirectorySeparatorChar);
_targetFramework = VersionUtility.ParseFrameworkFolderName(path);
}