Looping through a set of image resources (.resx) - c#

I've got a 3 sets of 9 images in seperate .resx files, and I'm trying to figure out how to loop a single set into 9 static picture boxes.
Loop through all the resources in a .resx file
I've looked through some of the solutions in the above link, like using ResXResourceReader, but it comes up with a parsing error when I use the GetEnumerator method.
When I use the ResourceSet resourceSet = MyResourceClass.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true); line, there's no definition for the ResourceManager within the Form class, or a GetResourceSet method when I create my own ResourceManager.
There is actually a method called CreateFileBasedResourceManager which I've dabbled in, but truth be told I don't understand the parameters it needs too well aside from the directory.
I've also looked at some of the solutions involving assemblies and retrieving the executing image assembly at runtime, but I think that's a little out of my depth at the moment.
Can anyone tell me what I'm doing wrong with the first two methods or maybe something entirely different?

Looking at MSDN, you should be able to iterate the values from a RESX file like so:
string resxFile = #".\CarResources.resx";
// Get resources from .resx file.
using (ResXResourceSet resxSet = new ResXResourceSet(resxFile))
{
// Retrieve the image.
Object image = resxSet.GetObject("NAMEOFFILE", true);
}
If you wanted to iterate all objects in the RESX file, you could do something like this:
using (ResXResourceReader resxReader = new ResXResourceReader(resxFile))
{
foreach (DictionaryEntry entry in resxReader) {
// entry.Key is the name of the file
// entry.Value is the actual object...add it to the list of images you were looking to keep track of
}
}
More can be found here.

I known that this is a old question, but today I got the same problem, and solve setting the BasePath property, like this:
oResReader = new ResXResourceReader(strInputFile);
oResReader.BasePath = Path.GetDirectoryName(strInputFile);
I found this solution here

Related

Alexa skill relative path to JSON file

I have a little problem with my Alexa skill. I want to include display components in it, and I want to send an appropriate directive. For most of my code, I use custom classes from API, e.g.
Resources colorsDark = new Resources();
colorsDark.description = "Color for dark theme";
colorsDark.when = "${viewport.theme == 'dark'}";
However, for one part of my skill, I use only previously-created values, so there is no need to create new objects and assign values to them. Instead, I've created a .json file that includes all the necessary information.
I'd like to point my code to this file, but here I encountered an issue.
I'd like to make it look like this:
doc.Styles = [JSON_FILE]
However, when the function is executed, it can't find this file.
I'm using JObject from Newtonsoft.Json.
I tried to use only the relative path:
JObject jObject = JObject.Parse(File.ReadAllText(".\\AlexaPresentationLanguage\\Styles\\ListStyle.json"));
as well as some other solutions like
Path.GetCurrentDirectory
and
Path.Combine
From System.IO
So far nothing worked. Do you have any ideas what can I do?

WPF - How to get code-behind Type definition for a Control from Pack URI?

Is there a method or a mapping in WPF that keeps this data? I would like to pass URI and to get a Type in return?
I am aware of Application.GetResourceStream(URI), but this returns BAML stream.
Thanks in advance.
After a little research, I did not find any method that returns the underlying type from the URI.
All that URI does is point to the resource that is embedded in a certain assembly, and I got what I need by extracting the resource manually:
public static Type GetTypeFromUri(Uri uri)
{
Type type = null;
using (var stream = Application.GetResourceStream(uri).Stream)
using (var reader = new Baml2006Reader(stream))
{
while (reader.Read())
{
if (reader.Type != null)
{
type = reader.Type.UnderlyingType;
break;
}
}
}
return type;
}
The following code works on a few assumptions:
Build action of the XAML resource is set to Page ( It is compiled to BAML )
The first type that is found by Baml2006Reader is the Code-Behind type ( For example if a class does not have a Code-Behind type like ResourceDictionary, you will get ResourceDictionary as the Type ). I am not greatly familiar with how BAML is structured, but a few tests have shown that this is the case.
Tests have shown that the reader first parses the included namespaces, and then the code-behind Type, therefore a few iterations of the reader.Read() are called (which have reader.Type empty, but reader.Namespace filled - which we don't need), and then the part which we need.
If we continued to read the stream, it would traverse down the visual tree, and parse children types.
Since Wikipedia says this:
At run-time, the framework engine extracts the .BAML file from assembly resources, parses it, and creates a corresponding WPF visual tree or workflow.
I believe it is the right way to do it.
If you believe this is not the most efficient way, or something should be done better, please give a new answer.

Put UI strings into resources

I followed the instructions in Put strings into resource files, instead of putting them directly in code or markup in Put UI strings into resources (except I don't understand step 4f). The structure in the Solution Explorer of the resources in my project is:
That is the hierarchy for the project's shared node. I opened the Resources1.resw file and added a couple of strings.
Then Add string resource identifiers to code and markup in that article has the following:
var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
When I have that I get:
WinRT information: ResourceMap Not Found.
I have tried many other possibilities that I have found from searching but either I get that error or the class or method (in other solutions) do not exist for my project. I assume there is something relevant missing from that article.
Using C#, how do I get strings from that resources file ?
You dont need to rename your resource file. If the name of the resource file isnt the default (Resources.resw), you can add the special name in the GetForCurrentView method.
In your case the call should be:
var loader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView("Resources1.resw");
Source:GetForCurrentView(System.String)
Did you tried to specify default language in app.xaml for your actual frame?
myFrame = new Frame
{
Language = Windows.Globalization.ApplicationLanguages.Languages[0]
};
Or more specific:
Windows.Globalization.ApplicationLanguages.Languages['en']

fastest way to search and delete files inside a directory

I've got a array of class in which one member is the full path to a file. I need to delete all those files from the directory which is not included in the array. As Usual, I am using the convential compare and delete method. I need to know if there any fast way to accomplish this.
I heard it can be done using Linq, but i dont have knowledge on linq.
My class struct is like below.
Class ImageDetails
{
public string Title;
public Boolean CanShow;
public String PathToFile;
}
I have an array of ImageDetails. The PathToFile contains full path
}
You can use Except() to handle this:
var filesToDelete = Directory.GetFiles(Path.GetDirectoryName(yourClass.FilePath)).Except(yourClass.TheArray);
Why do you need to compare? If you have the full file name, then
File.Delete(fileName);
is all you need. The file IO is likely to be the slowest part of this, so I don't think Linq will make much difference to the performance.
If the file may not exist, then check for that first:
if (File.Exists(fileName))
{
File.Delete(fileName);
}
Edit: I see you mean that you want to delete the file if it is not in the array. I read your question to mean that the directory is not included in the array.
Still, the actual file deletion is likely to be the slowest part of this.

Visual Studio Settings.settings file

Is there a way of creating a new setting in the Settings.settings file during run time?
For example, I want to write to the settings file from one class function, and read that value from a different class function. And no, I don't want to pass the values.
I know how to get values from the Settings.settings file
(value = Properties.Settings.Default.XXX)
I know how to update an existing value
(Properties.Settings.Default.XXX = newValue; Properties.Settings.Default.Save())
I want to know how I can add "Name", "Type", "Scope" and "Value" into the Settings.settings file during run time.
Any suggestions would be appreciated!
Thanks,
Ivar
The Issues
I believe Visual Studio generates code when you design the application settings and values, therefore at runtime this would not be easy and at worst impossible without a designer. However you can sometimes call upon design features at runtime.
You'll notice the code-behind has the properties in C# that you create in your designer. For example, I added a setting for:
Age [int] 30.
The code-behind has generated:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("30")]
public int Age {
get {
return ((int)(this["Age"]));
}
set {
this["Age"] = value;
}
}
(The code generation is why you have strongly-typed settings)
I'm unsure if you could effect this same thing at runtime. You would have to generate code and feed it back to the JIT compiler dynamically or something like that. Or maybe there's another way I don't know about in my limited understanding of settings.
Suggestion/Workaround
I'd suggest figuring out an alternate/easier way instead of jumping through hoops. For example, make one setting a collection type that is serializable so it can store multiple values.
Then you can, for example, store multiple ages under just one setting:
Ages [System.Collections.ArrayList] {add multiple values programatically}
You might end up with C# code to manage it like:
System.Collections.ArrayList list = new System.Collections.ArrayList();
list.Add("1");
list.Add("30");
Properties.Settings.Default.Ages = list;
Properties.Settings.Default.Save();

Categories

Resources