本文整理汇总了C#中System.Reflection.Assembly.GetManifestResourceStream方法的典型用法代码示例。如果您正苦于以下问题:C# Assembly.GetManifestResourceStream方法的具体用法?C# Assembly.GetManifestResourceStream怎么用?C# Assembly.GetManifestResourceStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.Assembly
的用法示例。
在下文中一共展示了Assembly.GetManifestResourceStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExtractResource
public void ExtractResource(string resourceName, string fullFileName, Assembly assembly)
{
_logger.Trace("Attempting to extract " + resourceName + "...");
string[] names = assembly.GetManifestResourceNames();
string resource = names.FirstOrDefault(x => x.Contains(resourceName));
if (resource == null)
{
throw new MissingManifestResourceException(resourceName);
}
Stream resourceStream = assembly.GetManifestResourceStream(resource);
Stream fileStream = File.OpenWrite(fullFileName);
byte[] buffer = new byte[resourceStream.Length];
resourceStream.Read(buffer, 0, buffer.Length);
fileStream.Write(buffer, 0, buffer.Length);
resourceStream.Close();
fileStream.Close();
_logger.Trace(resourceName + " extracted to " + fullFileName + ".");
}
示例2: LocalizedResource
public LocalizedResource(string resourceRoot, CultureInfo culture, Assembly assembly)
{
Stream istr = null;
string name = culture.Name.Replace('-', '_');
if (name != "") {
try {
istr = assembly.GetManifestResourceStream(resourceRoot + "_" + name + ".properties");
}
catch {}
if (istr == null) {
string[] nameSplit = name.Split(splt, 2);
if (nameSplit.Length == 2) {
try {
istr = assembly.GetManifestResourceStream(resourceRoot + "_" + nameSplit[0] + ".properties");
}
catch {}
}
}
}
if (istr == null) {
try {
istr = assembly.GetManifestResourceStream(resourceRoot + ".properties");
}
catch {}
}
if (istr != null) {
msgs.Load(istr);
}
List<string> keys = new List<string>(msgs.Keys);
foreach (string key in keys) {
string v = msgs[key];
StringBuilder sb = new StringBuilder();
int n = 0;
int state = 0;
foreach (char c in v) {
switch (state) {
case 0:
if (c == '%') {
state = 1;
break;
}
else if (c == '{' || c == '}') {
sb.Append(c);
}
sb.Append(c);
break;
case 1:
if (c == '%') {
sb.Append(c);
state = 0;
}
else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
state = 0;
sb.Append('{').Append(n++).Append('}');
break;
}
}
msgs[key] = sb.ToString();
}
}
示例3: ExecuteScript
protected virtual void ExecuteScript(PythonEngine engine, Assembly assembly, string script)
{
Stream stream = assembly.GetManifestResourceStream(script);
if (stream == null)
{
stream = assembly.GetManifestResourceStream(GetType().Namespace + "." + script);
}
if (stream == null)
{
throw new ApplicationException(
string.Format("Could not find script: {0} in the assembly resources", script));
}
using (stream)
using (StreamReader reader = new StreamReader(stream))
{
try
{
engine.Execute(reader.ReadToEnd());
}
catch (Exception ex)
{
throw new ApplicationException(string.Format("Failed to execute script: {0}", script), ex);
}
}
}
示例4: ReadResourceFromAssemblyImpl
public static Stream ReadResourceFromAssemblyImpl(Assembly asm, string resource)
{
// chop off the leading slash
resource = resource.Substring(1);
string mangledName = JVM.MangleResourceName(resource);
ManifestResourceInfo info = asm.GetManifestResourceInfo(mangledName);
if(info != null && info.FileName != null)
{
return asm.GetManifestResourceStream(mangledName);
}
Stream s = asm.GetManifestResourceStream(mangledName);
if(s == null)
{
Tracer.Warning(Tracer.ClassLoading, "Resource \"{0}\" not found in {1}", resource, asm.FullName);
throw new FileNotFoundException("resource " + resource + " not found in assembly " + asm.FullName);
}
switch (s.ReadByte())
{
case 0:
Tracer.Info(Tracer.ClassLoading, "Reading resource \"{0}\" from {1}", resource, asm.FullName);
return s;
case 1:
Tracer.Info(Tracer.ClassLoading, "Reading compressed resource \"{0}\" from {1}", resource, asm.FullName);
return new System.IO.Compression.DeflateStream(s, System.IO.Compression.CompressionMode.Decompress, false);
default:
Tracer.Error(Tracer.ClassLoading, "Resource \"{0}\" in {1} has an unsupported encoding", resource, asm.FullName);
throw new IOException("Unsupported resource encoding for resource " + resource + " found in assembly " + asm.FullName);
}
}
示例5: CheckAssembly
public static void CheckAssembly(Assembly target)
{
foreach (string resource in target.GetManifestResourceNames())
{
string ext = Path.GetExtension(resource).ToLower();
// Pluck the simple name of the resource out of
// the fully qualified string. tokens[tokens.Length - 1]
// is the file extension, also not needed.
// NB This works only for resource names that don't contain the period.
string[] tokens = resource.Split('.');
string name = tokens[tokens.Length - 2];
name = name.ToLower();
if (ext == ".bmp" || ext == ".gif" || ext == ".jpg" || ext == ".jpeg")
{
Bitmap bitmap = (Bitmap)Bitmap.FromStream(target.GetManifestResourceStream(resource));
bitmap.MakeTransparent(Color.White);
_bitmaps.Add(name, bitmap);
}
else if (ext == ".config" || ext == ".xml" || ext == ".txt" || ext == ".dll")
{
StreamReader sr = new StreamReader(target.GetManifestResourceStream(resource));
string fileText = sr.ReadToEnd();
_files.Add(name, fileText);
}
}
}
示例6: TryGetTextFromAssembly
public static bool TryGetTextFromAssembly(Assembly assembly, string resourceName, out string content,
string resourceDir = "conf")
{
try
{
string path = assembly.GetName().Name + "." + resourceDir + "." + resourceName;
using (Stream stream = assembly.GetManifestResourceStream(path))
{
if (stream != null)
{
using (StreamReader reader = new StreamReader(stream))
{
content = reader.ReadToEnd();
return true;
}
}
}
}
catch
{
}
content = string.Empty;
return false;
}
示例7: LoadTextFile
/// <summary>
/// Loads the named manifest resource as a text string.
/// </summary>
/// <param name="textFileName">Name of the text file.</param>
/// <returns>The contents of the manifest resource.</returns>
public static string LoadTextFile(string path, Assembly executingAssembly, bool autoAttachAssemblyName = true)
{
if (executingAssembly == null)
executingAssembly = Assembly.GetExecutingAssembly();
var pathToDots = path.Replace("\\", ".").Replace("/", ".");
string location;
if (autoAttachAssemblyName)
{
string assemblyName = executingAssembly.GetName().Name;
location = string.Format("{0}.{1}", assemblyName, pathToDots);
}
else
{
location = pathToDots;
}
using (var stream = executingAssembly.GetManifestResourceStream(location))
{
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
示例8: GetEmbeddedStringResource
/// <summary>
/// Gets the embedded file identified by the resource name, and converts the
/// file into a string.
/// </summary>
/// <param name="resourceName">In VS, is DefaultNamespace.FileName?</param>
/// <returns></returns>
public static string GetEmbeddedStringResource(Assembly assembly, string resourceName)
{
string result = null;
// Use the .NET procedure for loading a file embedded in the assembly
Stream stream = assembly.GetManifestResourceStream(resourceName);
if (stream != null)
{
// Convert bytes to string
byte[] fileContentsAsBytes = new byte[stream.Length];
stream.Read(fileContentsAsBytes, 0, (int)stream.Length);
result = Encoding.Default.GetString(fileContentsAsBytes);
}
else
{
// Embedded resource not found - list available resources
Debug.WriteLine("Unable to find the embedded resource file '" + resourceName + "'.");
Debug.WriteLine(" Available resources:");
foreach (string aResourceName in assembly.GetManifestResourceNames())
{
Debug.WriteLine(" " + aResourceName);
}
}
return result;
}
示例9: ExtractFileFromAssembly
private static void ExtractFileFromAssembly(Assembly currentAssembly, string resourceName, string saveAsName)
{
FileInfo fileInfoOutputFile = new FileInfo(saveAsName);
//CHECK IF FILE EXISTS AND DO SOMETHING DEPENDING ON YOUR NEEDS
if (fileInfoOutputFile.Exists)
{
}
FileStream streamToOutputFile = fileInfoOutputFile.OpenWrite();
//GET THE STREAM TO THE RESOURCES
Stream streamToResourceFile =
currentAssembly.GetManifestResourceStream(resourceName);
//---------------------------------
//SAVE TO DISK OPERATION
//---------------------------------
const int size = 4096;
byte[] bytes = new byte[4096];
int numBytes;
while ((numBytes = streamToResourceFile.Read(bytes, 0, size)) > 0)
{
streamToOutputFile.Write(bytes, 0, numBytes);
}
streamToOutputFile.Close();
streamToResourceFile.Close();
if (!File.Exists(saveAsName))
{
MessageBox.Show("Not found " + saveAsName);
}
}
示例10: ReadFromResourceName
public static string ReadFromResourceName(Assembly asm, string name)
{
using (var reader = new StreamReader(asm.GetManifestResourceStream(name)))
{
return reader.ReadToEnd();
}
}
示例11: GetIImageFromEmbeddedResource
public IImageWrapper GetIImageFromEmbeddedResource(string resourceName, Assembly asm)
{
var keyName = asm.GetManifestResourceNames().FirstOrDefault(p => p.EndsWith(resourceName));
IImage imagingResource;
var rstream = asm.GetManifestResourceStream(keyName);
if(rstream is MemoryStream)
{
using(var strm = (rstream as MemoryStream))
{
var pbBuf = strm.GetBuffer();
var cbBuf = (uint)strm.Length;
_factory.CreateImageFromBuffer(pbBuf, cbBuf, BufferDisposalFlag.BufferDisposalFlagNone, out imagingResource);
}
}else{
using(var strm = rstream)
{
var pbBuf = new byte[strm.Length];
strm.Read(pbBuf, 0, (int)strm.Length);
var cbBuf = (uint)strm.Length;
_factory.CreateImageFromBuffer(pbBuf, cbBuf, BufferDisposalFlag.BufferDisposalFlagNone, out imagingResource);
}
}
return new IImageWrapper(imagingResource);
}
示例12: GetPresetting
static public Preset[] GetPresetting(Assembly asmbl)
{
List<Preset> result = new List<Preset>();
foreach (string item in asmbl.GetManifestResourceNames())
{
ManifestResourceInfo resInfo = asmbl.GetManifestResourceInfo(item);
if (resInfo == null) continue;
using (Stream stream = asmbl.GetManifestResourceStream(item))
{
if (stream == null) continue;
if (item.Contains(".Resource.") && item.EndsWith(".xml"))
{
try
{
Preset preset;
XmlSerializer serializer = new XmlSerializer(typeof(Preset));
using (XmlReader reader = XmlReader.Create(stream))
preset = (Preset)serializer.Deserialize(reader);
result.Add(preset);
}
catch (Exception)
{
}
}
}
}
return result.ToArray();
}
示例13: GetImageResource
/// <summary>
/// extracts an embedded image from assembly
/// </summary>
/// <param name="resourcePath">path of the embedded resource</param>
/// <param name="assembly">source assemby</param>
/// <returns>initialized Image object</returns>
public static Image GetImageResource(string resourcePath, Assembly assembly)
{
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
{
return new Bitmap(stream);
}
}
示例14: GetResourceStream
private static Stream GetResourceStream(Assembly assembly, string resourceName)
{
var result = assembly.GetManifestResourceStream(resourceName);
if (result == null)
throw new InvalidOperationException(String.Format("Resource '{0}' not found.", resourceName));
return result;
}
示例15: GetFilePath
public static string GetFilePath(Assembly assembly, string resourceNamespace, string resourceName, bool keepOriginalFileName = false)
{
//Todo: not thread safe
var tempFolder = Path.GetTempPath();
var filePath = Path.Combine(tempFolder, resourceName + (keepOriginalFileName ? "" : ".condep"));
try
{
using (var stream = assembly.GetManifestResourceStream(resourceNamespace + "." + resourceName))
{
if(stream == null)
{
throw new ConDepResourceNotFoundException(string.Format("Unable to find resource [{0}]", resourceName));
}
using (var writeStream = File.Create(filePath))
{
stream.CopyTo(writeStream);
}
}
return filePath;
}
catch (Exception ex)
{
throw new ConDepResourceNotFoundException(string.Format("Resource [{0}]", resourceName), ex);
}
}