Closed Thread
Results 1 to 6 of 6

Thread: Creating multiple Streamwriter from single SaveAs TextBox entry

  1. #1
    shathway is offline Newbie
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Creating multiple Streamwriter from single SaveAs TextBox entry

    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Creating multiple Streamwriter from single SaveAs TextBox entry

    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.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  4. #3
    shathway is offline Newbie
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Re: Creating multiple Streamwriter from single SaveAs TextBox entry

    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".

  5. #4
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Creating multiple Streamwriter from single SaveAs TextBox entry

    Well then, just reuse the same dialog! Just change a few of its properties through the code, and voila!

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

  6. #5
    shathway is offline Newbie
    Join Date
    Oct 2008
    Posts
    5
    Rep Power
    0

    Re: Creating multiple Streamwriter from single SaveAs TextBox entry

    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.

  7. #6
    Join Date
    Mar 2008
    Location
    The North Pole
    Posts
    13,174
    Blog Entries
    13
    Rep Power
    114

    Re: Creating multiple Streamwriter from single SaveAs TextBox entry

    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.

    Quote Originally Posted by Jordan View Post
    Good members, like yourself, stick around and post for ages to come!
    Mr. Xav | Blog | Forums

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 02-07-2011, 11:29 AM
  2. Calling multiple functions in a single line
    By fazlerocks in forum C and C++
    Replies: 4
    Last Post: 01-17-2011, 06:03 PM
  3. Replies: 2
    Last Post: 10-28-2010, 03:15 PM
  4. Running multiple exe's in a single window
    By brandon21486 in forum C# Programming
    Replies: 2
    Last Post: 02-10-2010, 10:20 AM
  5. Multiple processes or single process?
    By sharat in forum Linux Programming and Scripting
    Replies: 1
    Last Post: 01-12-2010, 09:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts