Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Creating multiple Streamwriter from single SaveAs TextBox entry

streamwriter stream

  • Please log in to reply
5 replies to this topic

#1 shathway

shathway

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 29 October 2008 - 09:12 AM

I have a file that contains data from several companies. I would like to read the single file and then write it to separate company files. I use the SaveFile Dialog in my window to capture the directory path and the user assigned file name. Here is the code:

private void button2_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = saveFileDialog1.FileName;
}
}

I am new to C# programming so I am uncertain what is captured. But I transfer that to a StreamWriter here:

FileStream NetSysstream = new FileStream(textBox2.Text, FileMode.Create);
StreamWriter NetSyswriter = new StreamWriter(NetSysstream);

What I would like to do is use the info in textBox2 to create two separate StreamWriters. The directory would be the same for both, but the name would have some text indicating the company (i.e. textBox2(Comp1) and textBox2(Comp2)). I would then create separate FileStreams and StreamWriters for each (i.e. Comp1NetSysstream & Comp1NetSyswriter and Comp2NetSysstream & Comp2NetSyswriter).

I have tried to modify the textBox2.Text by assigning it to a variable and then use that variable in the FileStream and FileWriter statements but that does not seem to work. Any ideas?

Thanks in advance.
  • 0

#2 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 29 October 2008 - 10:12 AM

Use an OpenFileDialog to select the input file, then use a StreamReader to get the data inside the input file. Then just use a StreamWriter to write the information you need to a separate file.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 shathway

shathway

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 29 October 2008 - 10:41 AM

That is exactly what I am doing with the program today. I open the file with Open File dialog and write it to a new file that is reformatted. The problem is that the new file contains data for two different companies, say McD and Burger King. I can not share McD's data with Burger King and vice versa. So right now, I have to take the file I generated and split it manually.
I would like my program to split it for me. Read in one file with both McD and Burger King data. Output two files one with Burger King and the other with McD's data.
I could add a second SaveAs dialog to the program, but it seems more elagant to use one. I type in Save As "Financials Oct 08" and get the program to generate "Financials Oct 08 (McD).cvs" and "Financials Oct 08 (BK).cvs".
  • 0

#4 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 29 October 2008 - 10:46 AM

Well then, just reuse the same dialog! Just change a few of its properties through the code, and voila!
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 shathway

shathway

    CC Lurker

  • Just Joined
  • Pip
  • 5 posts

Posted 29 October 2008 - 11:33 AM

Changing it thru the code is what I do not know how to do. So let me be more explicit.

In the dialog, I type "Financial Oct 08".
The line textBox2.Text = saveFileDialog1.FileName; captures that name and the directory path.
textBox2.Text is transfered the the streamwriter in this line.
FileStream NetSysstream = new FileStream(textBox2.Text, FileMode.Create);

Now I have tried to change textBox2 line to read:
McDFile.Text = saveFileDialog1.FileName + "(McD)"
BKFile.Text = saveFileDialog1.FileName + "(BK)"
and then create two streamwriters:
FileStream NetSysstream = new FileStream(McDFile.Text, FileMode.Create);
FileStream NetSysstream = new FileStream(BKFile.Text, FileMode.Create);

When I do this, I get an error "name McDFile does not exist in current context".
Do I have to declare the name? Should it be a string?
I assign it inside a private routine attached to the Save button in the window. Then I use it inside a different private routine attached to a different button. Should I make these button pushes Public rather than private?
Will the FileStream method take a string as an argument? It seems that I have tried to declare string variables and then I get a different error caused when I try to use it in the FileStream method.
  • 0

#6 Xav

Xav

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 8356 posts

Posted 29 October 2008 - 12:05 PM

Hang on? You declare McDFile in a private routine?

That's the problem. If you declare it in a routine, you can only access it in the routine. Make it a global variable instead.

What do you want a FileStream for? Just use a StreamWriter to write the file.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download