I'm working on a program that can contain all the programs and games that you add to the program, so you can have it all in one place. The program also has a search feature.
The problem is that the program takes a while to load the first time you run it, or after you restart the computer. I am pretty sure it is because I use Icon.ExtractAssociatedIcon to get the icon from the executables.
So if there is a lot in the XML file (I save data in an XML file) then it takes a while to load the program because i extract the executable icon.
I made a method to make it easier for me:
public static BitmapImage GetExeIcon(string path)
{
try
{
using (MemoryStream memory = new MemoryStream())
{
System.Drawing.Icon.ExtractAssociatedIcon(path).ToBitmap().Save(memory, System.Drawing.Imaging.ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImg = new BitmapImage();
bitmapImg.BeginInit();
bitmapImg.StreamSource = memory;
bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
bitmapImg.EndInit();
return bitmapImg;
}
}
catch (Exception)
{
MessageBox.Show("Are you sure it is a valid exe path?");
}
return null;
}
I will admit, there's some of the stuff in the using blocks i don't understand, but i try to understand it as much as i can.
Load method:
public void Load()
{
if(File.Exists("ApplicationSaves\\apps.xml"))
{
using (XmlReader xmlReader = XmlReader.Create("ApplicationSaves\\apps.xml"))
{
string name = string.Empty;
string desc = string.Empty;
ApplicationItem.AppType type = ApplicationItem.AppType.Game;
string exePath = string.Empty;
while (xmlReader.Read())
{
switch (xmlReader.Name)
{
case "Name":
name = xmlReader.ReadElementContentAsString();
break;
case "Description":
desc = xmlReader.ReadElementContentAsString();
break;
case "Type":
type = (xmlReader.ReadElementContentAsString() == "Game" ? ApplicationItem.AppType.Game : ApplicationItem.AppType.Program);
break;
case "ExePath": //This is the last data it needs
exePath = xmlReader.ReadElementContentAsString();
GlobalManager.applicationList.Add(new ApplicationItem() { Name = name, Description = desc, ExePath = exePath, Type = type, Icon = GlobalManager.GetExeIcon(exePath) });
break;
}
}
}
}
}
You can see i use GlobalManager.GetExeIcon(exePath) in the case "ExePath" at the very end, so it does it to all the items it add
Last picture you can see what i use the icons for :-) Do you have any advice how i could reduce load time? I know i can save the extracted icons to a folder and load them from there if they exist, i guess that will reduce the loading a little. But if you know a better way, i will appreciate it :-)
PS. Sorry if it is too much to read, i'm always trying to be specific, but with a lot of text. Please tell me if you need more information.
You don't have to read the stuff below, but maybe someone that reads it has some advice:
I won't say i'm new to C#(i started about 2 years ago) or programming in general. 2 years with C# and i still don't know how/when/why to use structs, interfaces, and other cool stuff.
I've been taking long breaks sometimes because i didn't know what i should do, and losing motivation because i felt i was learning nothing after many months(I still do lose motivation sometimes because i feel i'm not good enough or not learning)