I need to check whether an embedded resource exists. In WPF this i relatively easy, but in WP7 I get a MissingManifestResourceException
The WPF code which works is:
public static IEnumerable<object> GetResourcePaths(Assembly assembly) {
var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
//var resourceName = assembly.GetName().Name + ".g";
var assemblyName = assembly.FullName.Split(',')[0];
var resourceName = assemblyName + ".g";
var resourceManager = new ResourceManager(assemblyName, assembly);
try {
var resourceSet = resourceManager.GetResourceSet(culture, true, true);
foreach (System.Collections.DictionaryEntry resource in resourceSet) {
yield return resource.Key;
}
} finally {
resourceManager.ReleaseAllResources();
}
}
I tried replacing it with the code below, which resulted in the exception (on line 9). Is there a way to do this in Silverlight / WP7?
public static IEnumerable<object> GetResourcePaths(Assembly assembly) {
var culture = System.Threading.Thread.CurrentThread.CurrentCulture;
//var resourceName = assembly.GetName().Name + ".g";
var assemblyName = assembly.FullName.Split(',')[0];
var resourceName = assemblyName + ".g";
var resourceManager = new ResourceManager(assemblyName, assembly);
try {
var resourceSet = resourceManager.GetResourceSet(culture, true, true);
foreach (System.Collections.DictionaryEntry resource in resourceSet) {
yield return resource.Key;
}
} finally {
resourceManager.ReleaseAllResources();
}
}
The answer to this previous question: WP7: collection of images seems to indicate that you might need to get a stream before calling GetResourceSet:
var NOT_USED = rm.GetStream("app.xaml"); // without getting a stream, next statement doesn't work - bug?
Sounds a bit hacky, but if it works ;)
Related
I have this problem when I'm trying to read JSON file (or any file): It's not able to find that file. I try everything, even the absolute path (error almost same - DirectoryNotFound)
This is structure of mine code:
And this is code:
private void LoadJson()
{
using (var r = new StreamReader("quizQuestions.json"))
{
string json = r.ReadToEnd();
items = JsonConvert.DeserializeObject<List<Questions>>(json);
}
}
I I even try to use Directory.GetCurrentDirectory() but it's returning : / -> only this character. I don't know where is a mistake or if I forgot to set something. I try to find answers everywhere but I was not able to find anything with this.
Make sure the Build Action of the file is set as Content or as an Asset and give this a try.
private void LoadJson()
{
AssetManager assets = this.Assets;
using (var r = new StreamReader(assets.Open ("quizQuestions.json")))
{
string json = r.ReadToEnd();
items = JsonConvert.DeserializeObject<List<Questions>>(json);
}
}
You can configure the file as Embedded Resource and then access it like this:
public static Stream GetEmbeddedResourceStream(Assembly assembly, string resourceFileName)
{
var resourceNames = assembly.GetManifestResourceNames();
var resourcePaths = resourceNames
.Where(x => x.EndsWith(resourceFileName, StringComparison.CurrentCultureIgnoreCase)).ToArray();
if (resourcePaths.Any() && resourcePaths.Count() == 1)
{
return assembly.GetManifestResourceStream(resourcePaths.Single());
}
return null; // or throw Exception
}
private void LoadJson()
{
Assembly assembly = GetAssemblyContainingTheJson();
using (var r = GetEmbeddedResourceStream(assembly, "quizQuestions.json"))
{
string json = r.ReadToEnd();
items = JsonConvert.DeserializeObject<List<Questions>>(json);
}
}
I have an UnmanagedMemoryStream in below code, How can I get the object from deserilizing it. I was trying to get a Resource (rd.xaml) from an Assembly :
string address = #"WpfControlLibrary1.dll";
Assembly skinAssembly = Assembly.LoadFrom(address);
string name = skinAssembly.GetName().Name +".g";
var manager = new ResourceManager(name, skinAssembly);
ResourceSet rs = manager.GetResourceSet(CultureInfo.CurrentCulture, true, true);
UnmanagedMemoryStream stream = (UnmanagedMemoryStream)rs.GetObject("rd.baml", true);
I'm not able to deserialize the content of Stream into a .net Object(which is a Resource Dictionary in above case). How can I do this?
PS: BinaryFormatter is throwing an exception while Deserialize operation.
Below is how I've done it, with optimized code:
public ResourceDictionary GetResourceDictionary(string assemblyName)
{
Assembly asm = Assembly.LoadFrom(assemblyName);
Stream stream = asm.GetManifestResourceStream(asm.GetName().Name + ".g.resources");
using (ResourceReader reader = new ResourceReader(stream))
{
foreach (DictionaryEntry entry in reader)
{
var readStream = entry.Value as Stream;
Baml2006Reader bamlReader = new Baml2006Reader(readStream);
var loadedObject = System.Windows.Markup.XamlReader.Load(bamlReader);
if (loadedObject is ResourceDictionary)
{
return loadedObject as ResourceDictionary;
}
}
}
return null;
}
OUTPUT:
I wanted to read a ResourceDictionary from an External Assembly/Another Project, So that I can iterate it's Resources to
use.
Currently I'm working on a custom importer for Ironpython, which should add an abstraction layer for writing custom importer. The abstraction layer is an IronPython module, which bases on PEP 302 and the IronPython zipimporter module. The architecture looks like this:
For testing my importer code, I've written a simple test package with modules, which looks like this:
/Math/
__init__.py
/MathImpl/
__init__.py
__Math2__.py
/Math/__init__.py:
print ('Import: /Math/__init__.py')
/Math/MathImpl/__init__.py:
# Sample math package
print ('Begin import /Math/MathImpl/__init__.py')
import Math2
print ('End import /Math/MathImpl/__init__.py: ' + str(Math2.add(1, 2)))
/Math/MathImpl/Math2.py:
# Add two values
def add(x, y):
return x + y
print ('Import Math2.py!')
If i try to import MathImpl like this in a script: import Math.MathImpl
My genericimporter get's called and searchs for some module/package in the find_module method. Which returns an instance of the importer if found, else not:
public object find_module(CodeContext/*!*/ context, string fullname, params object[] args)
{
// Set module
if (fullname.Contains("<module>"))
{
throw new Exception("Why, why does fullname contains <module>?");
}
// Find resolver
foreach (var resolver in Host.Resolver)
{
var res = resolver.GetModuleInformation(fullname);
// If this script could be resolved by some resolver
if (res != ResolvedType.None)
{
this.resolver = resolver;
return this;
}
}
return null;
}
If find_module is called the first time,fullname contains Math, which is ok, because Math should be imported first. The second time find_module is called, Math.MathImpl should be imported, the problem here is, that fullname has now the value <module>.MathImpl, instead of Math.MathImpl.
My idea was, that the module name (__name__) is not set correctly when Math was imported, but i set this in any case when importing the module in load_module:
public object load_module(CodeContext/*!*/ context, string fullname)
{
string code = null;
GenericModuleCodeType moduleType;
bool ispackage = false;
string modpath = null;
PythonModule mod;
PythonDictionary dict = null;
// Go through available import types by search-order
foreach (var order in _search_order)
{
string tempCode = this.resolver.GetScriptSource(fullname + order.Key);
if (tempCode != null)
{
moduleType = order.Value;
code = tempCode;
modpath = fullname + order.Key;
Console.WriteLine(" IMPORT: " + modpath);
if ((order.Value & GenericModuleCodeType.Package) == GenericModuleCodeType.Package)
{
ispackage = true;
}
break;
}
}
// of no code was loaded
if (code == null)
{
return null;
}
var scriptCode = context.ModuleContext.Context.CompileSourceCode
(
new SourceUnit(context.LanguageContext, new SourceStringContentProvider(code), modpath, SourceCodeKind.AutoDetect),
new IronPython.Compiler.PythonCompilerOptions() { },
ErrorSink.Default
);
// initialize module
mod = context.ModuleContext.Context.InitializeModule(modpath, context.ModuleContext, scriptCode, ModuleOptions.None);
dict = mod.Get__dict__();
// Set values before execute script
dict.Add("__name__", fullname);
dict.Add("__loader__", this);
dict.Add("__package__", null);
if (ispackage)
{
// Add path
string subname = GetSubName(fullname);
string fullpath = string.Format(fullname.Replace(".", "/"));
List pkgpath = PythonOps.MakeList(fullpath);
dict.Add("__path__", pkgpath);
}
else
{
StringBuilder packageName = new StringBuilder();
string[] packageParts = fullname.Split(new char[] { '/' });
for (int i = 0; i < packageParts.Length - 1; i++)
{
if (i > 0)
{
packageName.Append(".");
}
packageName.Append(packageParts[i]);
}
dict["__package__"] = packageName.ToString();
}
var scope = context.ModuleContext.GlobalScope;
scriptCode.Run(scope);
return mod;
}
I hope some one has an idea, why this happens. A few line which also may cause the problem are:
var scriptCode = context.ModuleContext.Context.CompileSourceCode
(
new SourceUnit(context.LanguageContext, new SourceStringContentProvider(code), modpath, SourceCodeKind.AutoDetect),
new IronPython.Compiler.PythonCompilerOptions() { },
ErrorSink.Default
);
and
mod = context.ModuleContext.Context.InitializeModule(modpath, context.ModuleContext, scriptCode, ModuleOptions.None);
Because i don't know, whether creating a module this way is completly correct.
The problem can be reproduced downloading this project/branch: https://github.com/simplicbe/Simplic.Dlr/tree/f_res_noid and starting Sample.ImportResolver. An exception in find_module will be raised.
Thank you all!
This problem is solved. Modpath what not allowed to contains /. In general only chars were allowed, which also can be in a file-name.
Maybe this is helpful for someone else...
I need to make a WMI call in the constructor of my service. But when I start/restart the system this call takes significant amount of time.
I am using the following code to get the path of the windows service....
Here I've used the EnumerationOptions to improve the query performance, now in order to use it I have to use the ManagementScope which is "root\civm2", every time I've to use "root'civm2" as Management scope,
Earlier I was using managementObjectCollection.Count to know whether it contains any items or not, now to improve the performance I am using managementObjectEnumerator.MoveNext, will it help, I've commented the count related code.
Is there any better way to improve the performance of the same code...
EnumerationOptions options = new EnumerationOptions();
// options.Rewindable = false; **// I HAVE TO COMMENT OUT THIS IN ORDER TO GET THE RESULTS....**
options.ReturnImmediately = true;
string query = string.Format("SELECT PathName FROM Win32_Service WHERE Name = '{0}'", "MyService");
ManagementScope ms12 = new ManagementScope(#"root\cimv2");
ms12.Connect();
using (var managementObjectSearcher = new ManagementObjectSearcher(query))
{
managementObjectSearcher.Scope = ms12;
managementObjectSearcher.Options = options;
var managementObjectCollection = managementObjectSearcher.Get();
//if (managementObjectCollection.Count > 0)
//{
var managementObjectEnumerator = managementObjectCollection.GetEnumerator();
if (managementObjectEnumerator.MoveNext())
{
var invalidChars = new Regex(string.Format(CultureInfo.InvariantCulture, "[{0}]", Regex.Escape(new string(Path.GetInvalidPathChars()))));
var path = invalidChars.Replace(managementObjectEnumerator.Current.GetPropertyValue("PathName").ToString(), string.Empty);
Console.WriteLine(path);
}
//}
else
{
Console.WriteLine("Else part...");
}
}
Am I using the scope and EnumerationOption in correct way??
Please guide.
As the answer to your another question suggest you can build the object path of the class and use the ManagementObject directly to improve the performance , now if you want to check if the ManagementObject return an instance you can use the private property IsBound.
string ServicePath = string.Format("Win32_Service.Name=\"{0}\"", "MyService");
var WMiObject = new ManagementObject(ServicePath);
PropertyInfo PInfo = typeof(ManagementObject).GetProperty("IsBound", BindingFlags.NonPublic | BindingFlags.Instance);
if ((bool)PInfo.GetValue(WMiObject, null))
{
string PathName = (string)WMiObject.GetPropertyValue("PathName");
var invalidChars = new Regex(string.Format(CultureInfo.InvariantCulture, "[{0}]", Regex.Escape(new string(Path.GetInvalidPathChars()))));
var path = invalidChars.Replace(PathName, string.Empty);
Console.WriteLine(path);
}
else
{
Console.WriteLine("Else part...");
}
It appears that in more recent versions of the .NET framework the binding happens as late as possible. At least this was the case for me when I was testing the existence of a specific shared folder.
Here is an update to #RRUZ 's solution which uses a try-catch instead of reflecting the IsBound internal property.
var servicePath = string.Format("Win32_Service.Name=\"{0}\"", "MyService");
string pathName = null;
try
{
var wmiObject = new ManagementObject(servicePath);
pathName = (string)wmiObject.GetPropertyValue("PathName");
}
catch {}
if (pathName != null)
{
var invalidChars = new Regex(string.Format(CultureInfo.InvariantCulture, "[{0}]", Regex.Escape(new string(Path.GetInvalidPathChars()))));
var path = invalidChars.Replace(pathName, string.Empty);
Console.WriteLine(path);
}
else
{
Console.WriteLine("Else part...");
}
I'm developing a localisable application. In my "local" resource file, I've the language used by default (english) and if possible, I load the user's preference and culture and load strings translated in is language.
So what I've done :
private static CultureInfo _culture = CultureInfo.CurrentUICulture;
private static ResourceManager _manager;
private static void ToNeutralCulture()
{
while (!_culture.IsNeutralCulture)
{
_culture = _culture.Parent;
}
}
private static void LoadCulture()
{
ResourceManager manager = Properties.Resources.ResourceManager;
try
{
ToNeutralCulture();
string assembly = Assembly.GetCallingAssembly().GetName().CodeBase;
string assemblyDir = Path.GetDirectoryName(assembly);
string assemblyName = Path.GetFileNameWithoutExtension(assembly);
string resourceFileName = string.Format(CultureInfo.InvariantCulture,
#"{0}\{1}_{2}.dll",
assemblyDir,
assemblyName,
_culture.Name.ToUpper());
FileInfo resourceFile = new FileInfo(resourceFileName);
if (resourceFile.Exists)
{
Assembly resourceAssembly = Assembly.LoadFrom(resourceFile.FullName);
string[] manifests = resourceAssembly.GetManifestResourceNames();
if (manifests.Length == 1)
{
manager = new ResourceManager(manifests[0], resourceAssembly);
}
using (ResourceReader reader = new ResourceReader(resourceAssembly.GetManifestResourceStream(manifests[0])))
{
IDictionaryEnumerator dict = reader.GetEnumerator();
while (dict.MoveNext())
{
string key = dict.Key as string;
object val = dict.Value;
//string mVal = manager.GetString(key);
}
}
}
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
Trace.WriteLine(string.Format(CultureInfo.InvariantCulture,
"Fail to loading culture {0}",
(_culture == null) ? "--" : _culture.EnglishName));
}
_manager = manager;
}
Assembly is correctly loaded and the enumerator will display me all resources present in the resource file, well, works fine except :
string mVal = manager.GetString(key);
When I uncommented this line, I've an System.Resources.MissingManifestResourceException, can someone tell me why?
Thanks !
[EDIT]
Project "MyApp"
namespace MyApp
{
Assembly resourceAssembly = Assembly.LoadFrom(resourceFileName);
string[] manifests = resourceAssembly.GetManifestResourceNames();
if (manifests.Length == 1)
{
manager = new ResourceManager(manifests[0], resourceAssembly);
}
// Throws the exception
manager.GetString("PleaseCallIT", null);
// Works
using (ResourceReader reader = new ResourceReader(resourceAssembly.GetManifestResourceStream(manifests[0])))
{
IDictionaryEnumerator dict = reader.GetEnumerator();
while (dict.MoveNext())
{
string key = dict.key as string; // PleaseCallIT
object val = dict.value; // Please call IT.
}
}
}
Project "MyApp_FR" (Resources.Designer.cs auto-generated file)
namespace MyApp.Properties {
// ...
internal static string PleaseCallIT {
get {
return ResourceManager.GetString("PleaseCallIT", resourceCulture);
}
}
}
I don't understand...
I found why, hope this will help someone that is in the same case.
So, I looked in MyApp_FR.dll the code generated to use the Resource file, it is :
new global::System.Resources.ResourceManager("MyApp_FR.Properties.Resources", typeof(Resources).Assembly);
but when retrieving the manifest file names, I got :
"MyApp_FR.Properties.Resources.resources"
Seems to be there is a .resource to much in this room... By removing it, I can use my ResourceManager normally, all works fine...
Final code :
Assembly resourceAssembly = Assembly.LoadFrom(resourceFileName);
string[] manifests = resourceAssembly.GetManifestResourceNames();
if (manifests.Length == 1)
{
string manifest = manifests[0].Replace(".resources", string.Empty);
manager = new ResourceManager(manifest, resourceAssembly);
}
// Works !
manager.GetString("PleaseCallIT", null);
From Microsoft Support:
This problem occurs if you use a localized resource that exists in a satellite assembly that you created by using a .resources file that has an inappropriate file name. This problem typically occurs if you manually create a satellite assembly:
Try this KB:
http://support.microsoft.com/kb/839861
An alternate approach, put in the following as test code:
string[] resources =
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
In debugging, check the contents of resources to see if it matches what you are loading with ResourceManager.
Especially note, if you get something like 'MyAssembly..Resources.resources', then you will need to explicitly add 'Resources' to the ResourceManager constructor:
private static readonly ResourceManager stringTable =
new ResourceManager("MyAssembly.Resources", Assembly.GetExecutingAssembly());