1

I'm trying to set up my form to load its BackgroundImagefrom a path listed in a .txt file.

The contents of the text file look like this:

System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Background.png")

When the form loads, I run this code:

//LOAD FORM

     string BackgroundSkinsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Skin.cfg");
     this.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));

The contents of that text file can be changed by the user clicking on a button relating to a different background image:

//CHANGE BG IMAGE

    private void ChangeBGButton_Click(object sender, EventArgs e)
    {  
     string BackgroundSkinsPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Skin.cfg");
     System.IO.File.WriteAllText(BackgroundSkinsPath, "System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Background2.png")");
     this.BackgroundImage = Image.FromFile(System.IO.File.ReadAllText(BackgroundSkinsPath));
    }

The reason I want to write the background image location to a file, is so that the next time the user loads the application, it will have the background image they've chosen, and this is the only way I can think of to do this.

The problem is, the code I've written doesn't work, and I'm not sure how to fix it.

I want the user to be able to choose a background image for the application from a folder in their "My Documents" folder (via a button or similar), and have the application remember which image they chose the next time they start the application.

Patrick
  • 430
  • 6
  • 21

1 Answers1

1

You are writing a C# expression in your cfg file, not the literal string with the effective path of the background image.

using System.IO;

private void ChangeBGButton_Click(object sender, EventArgs e)
{  
    string BackgroundSkinsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Skin.cfg");
    string currentBackgroundImage = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Background2.png");
    File.WriteAllText(BackgroundSkinsPath, currentBackgroundImage );
    this.BackgroundImage = Image.FromFile(File.ReadAllText(currentBackgroundImage));
}

Now in your LOAD method you could use

string BackgroundSkinsPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDoc‌​uments), "Application Name", "Skins", "Skin.cfg");
this.BackgroundImage = Image.FromFile(File.ReadAllText(BackgroundSkinsPath));
Steve
  • 213,761
  • 22
  • 232
  • 286
  • That worked great, Thanks! I knew it was something small I'd be missing in there lol – Patrick Jan 01 '16 at 22:29
  • @Patrick I suggest to post a new question, not updating this one with a second question. You will have more people looking at your new question – Steve Jan 01 '16 at 23:21
  • [Done](http://stackoverflow.com/questions/34561210/change-backgroundimage-of-form-from-custom-control-running-in-panel) – Patrick Jan 01 '16 at 23:45
  • Is there any way I could use this same method for background images that are in my projects resources? The reason I ask is because I'd like to have "stock" skins loaded from a resource and "custom" ones loaded from My Documents. If you think it would be better to make this a separate question, I will. – Patrick Jan 02 '16 at 17:09
  • I always suggest you to post a new question if you are able to detail well your problem. Instead posting additional 'problems' to an already answered question is the basics of [Chamaleon Questions](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions) and it is often of little help because no one else will be informed of your new 'problem'. However, I am not saying this just to discard your request, but truly because it is the best thing to do. – Steve Jan 02 '16 at 17:25
  • If you wouldn't mind taking a look at [THIS](http://stackoverflow.com/questions/34568797/user-selectable-backgroundimage-from-settings-file-physical-and-resource), it would be much appreciated. Thanks for all your help so far. – Patrick Jan 02 '16 at 17:46