save file - open file - baby steps

  • Thread starter Marge Inoferror
  • Start date
M

Marge Inoferror

Visual C#...

I'd like to save some settings that are in integers and then save some lists
of file names. I'm just starting off (as you can tell - no doubt.).

I'm starting by trying to save two integers: numRows and numCols.

I put a saveFileDialog in my design view. When it's clicked I do this:

private void clickSaveFile(object sender, System.EventArgs e)
{
DialogResult buttonClicked = saveFileDialog.ShowDialog();
DialogResult buttonClicked = saveFileDialog.ShowDialog();
if (buttonClicked.Equals(DialogResult.OK))
{
Stream saveStream =saveFileDialog.OpenFile();
StreamWriter saveWriter = new StreamWriter(saveStream);
saveWriter.Write(numRows);
saveWriter.Write(numCols);
saveWriter.Close();
}
}

This converts the integers to strings, I believe.

I try to retrieve the file:

private void clickFileOpen(object sender, System.EventArgs e)
{
string numRowsString;
string numColsString;
DialogResult buttonClicked = openFileDialog.ShowDialog();
if (buttonClicked.Equals(DialogResult.OK))
{
Stream openStream =openFileDialog.OpenFile();
StreamReader openReader = new StreamReader(openStream);
numRowsString = openReader.ReadLine();
numColsString = openReader.ReadLine();
openReader.Close();
/* now what ?? */

}
}

How do I convert the strings to ints? Is there a better way to save and
retrive the file?

Thanks,
Marge
 
M

Morten Wennevik

Hi Marge,

Reply below ...

Visual C#...

I'd like to save some settings that are in integers and then save some lists
of file names. I'm just starting off (as you can tell - no doubt.).

I'm starting by trying to save two integers: numRows and numCols.

I put a saveFileDialog in my design view. When it's clicked I do this:

private void clickSaveFile(object sender, System.EventArgs e)
{
DialogResult buttonClicked = saveFileDialog.ShowDialog();
DialogResult buttonClicked = saveFileDialog.ShowDialog();

I assume you only use one of the ShowDialogs as otherwise the result of the first one will be lost :p
Btw, you can put them on the same line with

if(saveFileDialog.ShowDialog() == DialogResult.OK)
if (buttonClicked.Equals(DialogResult.OK))
{
Stream saveStream =saveFileDialog.OpenFile();
StreamWriter saveWriter = new StreamWriter(saveStream);
saveWriter.Write(numRows);
saveWriter.Write(numCols);

You should use WriteLine instead of Write or both numbers will end up on the same line as a longer one
12 and 14 will be 1214 if you use Write. Or you can use Write(numRows + "\r\n") to force a linebreak.
saveWriter.Close();
}
}

This converts the integers to strings, I believe.

Yes, StreamWriter or StreamReader will only use strings
I try to retrieve the file:

private void clickFileOpen(object sender, System.EventArgs e)
{
string numRowsString;
string numColsString;
DialogResult buttonClicked = openFileDialog.ShowDialog();
if (buttonClicked.Equals(DialogResult.OK))
{
Stream openStream =openFileDialog.OpenFile();
StreamReader openReader = new StreamReader(openStream);
numRowsString = openReader.ReadLine();
numColsString = openReader.ReadLine();
openReader.Close();
/* now what ?? */

Now you use the handy Parse method that follow all the number classes and a few others.
Or you can use the Convert static class.

Since the numbers are originally integers you can either use

int number = Int32.Parse(string);

or

int number = Convert.ToInt32(string);

Now, you need to be aware that both of these will throw an exception if the string isn't a valid integer so unless there is no way they aren't a valid integer you should put the line in a try/catch block.

Alternatly you can use Double.TryParse which does not throw an exception if it fails, but is more cumbersome to use than the above.
}
}

How do I convert the strings to ints? Is there a better way to save and
retrive the file?

Thanks,
Marge

That depends on your need, but this is an easy way since you can edit the config file manually, in which case any number entered will be strings.

Also, StreamReader and StreamWriter can load a FileStream in their own constructor so you don't need to create a separate Stream first.

StreamWriter sw = new StreamWriter(saveFileDialog.FileName);
StreamReader sr = new StreamReader(openFileDialog.FileName);
 
M

Marge Inoferror

Morten,

Thank you very much!

Marge



Morten Wennevik said:
Hi Marge,

Reply below ...

I assume you only use one of the ShowDialogs as otherwise the result of the first one will be lost :p
Btw, you can put them on the same line with

if(saveFileDialog.ShowDialog() == DialogResult.OK)


You should use WriteLine instead of Write or both numbers will end up on the same line as a longer one
12 and 14 will be 1214 if you use Write. Or you can use Write(numRows + "\r\n") to force a linebreak.


Yes, StreamWriter or StreamReader will only use strings


Now you use the handy Parse method that follow all the number classes and a few others.
Or you can use the Convert static class.

Since the numbers are originally integers you can either use

int number = Int32.Parse(string);

or

int number = Convert.ToInt32(string);

Now, you need to be aware that both of these will throw an exception if
the string isn't a valid integer so unless there is no way they aren't a
valid integer you should put the line in a try/catch block.
Alternatly you can use Double.TryParse which does not throw an exception
if it fails, but is more cumbersome to use than the above.
That depends on your need, but this is an easy way since you can edit the
config file manually, in which case any number entered will be strings.
Also, StreamReader and StreamWriter can load a FileStream in their own
constructor so you don't need to create a separate Stream first.
 
Top