473,322 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

save file - open file - baby steps

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
Nov 16 '05 #1
2 20703
Hi Marge,

Reply below ...

On Tue, 03 Aug 2004 02:47:59 GMT, Marge Inoferror <in*****************@yahoo.com> wrote:
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);

--
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Morten,

Thank you very much!

Marge

"Morten Wennevik" <Mo************@hotmail.com> wrote in message
news:opsb45gf0bklbvpo@morten_x.edunord...
Hi Marge,

Reply below ...

On Tue, 03 Aug 2004 02:47:59 GMT, Marge Inoferror <in*****************@yahoo.com> wrote:
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);

--
Happy coding!
Morten Wennevik [C# MVP]

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Artemisio | last post by:
I have done a small currency calculator. It works and I'm very glad. But...I'd like to have a line shift if user types a wrong choice. Please, look at the code and output example down here: #...
0
by: Eric | last post by:
hey folks, I'm using the development environment 7.1.3088 and since installation I've been experiencing slow access times (1-2 mins per folder) on opening folders, going up a folder level when...
2
by: gary smith | last post by:
I am trying to open a file (file --> open --> file) at an FTP location using visual Studio.net. Unfortunately, I get an "unspecified error" alert whenever I select a file. Can anyone help...
4
by: Satya | last post by:
I am trying to display a PDF file (which I am being passed from a web service as a binary stream) in a browser, but I am being prompted to save the file instead. I don't want the user to be...
3
by: mo | last post by:
I have an application that uses Reporting Services. When the user chooses to print a report, they are taken to a window that allows them to fill in parameters for the report. They then click a...
0
by: U S Contractors Offering Service A Non-profit | last post by:
Baby steps to the "New" New Orleans Inbox Reply Craig Somerford to Chasing, Donald, me, (bcc:Discovery), (bcc:Beijing) show details 3:55 pm (0 minutes ago) Our Beloved New Orleans needs you...
4
by: Kaustubh Budukh | last post by:
I am designing a VB.NET based website I have a hyperlink, upon clicking on it a "File Download" prompt opens which asks me whether I want to "Save" the file, "Open" But I want it to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.