-1

I would like to load my form size,background image,button position ect.. from a text file at launch.

What I would like to do is say something like this below.

StreamReader streamReader = new StreamReader(appPath + @"\Config\Launcher.txt");
string size = streamReader.ReadLine();
this.Size = new Size(size);
streamReader.Close();

Now I understand I must parse the string to an int in some way then pass that to the size bit.

How would I go about doing this thanks.

Del

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Chossy
  • 65
  • 1
  • 7
  • Have you searched for "parse string to int in C#" ? Apart from that, the `Size` structure has [two constructores](http://msdn.microsoft.com/en-us/library/System.Drawing.Size.Size.aspx), the first takes a `Point` and the other takes **two** integers. – Tim Schmelter Sep 20 '13 at 08:30

2 Answers2

1

You should use configuration files. So Web.config or App.config Also use the configuration manager.

Your file should look like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <window width="800" height="600" />
</configuration>

and your code should look something like this (untested):

this.Size = new Size(ConfigurationManager.window["width"], ConfigurationManager.window["height"]);
Community
  • 1
  • 1
MrFox
  • 4,852
  • 7
  • 45
  • 81
1

Size has Width and Height. Do you have a square, or how do you distinguish height from width?

to parse string into int and use it for a square, you can use:

string size = streamReader.ReadLine();
int iSize = 0;
if (int.TryParse(size, out iSize)) {
     this.Size = new Size(iSize, iSize);
} else {
 // error, maybe load default size
}

You can also use configuration files like App.config

Arnold Waldraf
  • 160
  • 3
  • 13