By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,187 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,187 IT Pros & Developers. It's quick & easy.

Multiple File Names Created From One Streamwriter

P: n/a
TC
I need the ability to create a series of individual files via one
Streamwriter (one at a time). Currently I have an application that is
writing out all of the data to one text file. The user has requested the
data is to be broken into individual files (could be from one to many depend
on the volume of data). This could lead to many separate files being
created. How do I write to a new file name using “StreamWriter sr =
File.CreateText(FileName)” ?

Thanks
May 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
TC,

All you have to do is create a new FileStream, and then pass that to a
new StreamWriter.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"TC" <TC@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
I need the ability to create a series of individual files via one
Streamwriter (one at a time). Currently I have an application that is
writing out all of the data to one text file. The user has requested the
data is to be broken into individual files (could be from one to many
depend
on the volume of data). This could lead to many separate files being
created. How do I write to a new file name using "StreamWriter sr =
File.CreateText(FileName)" ?

Thanks

May 15 '06 #2

P: n/a
TC <TC@discussions.microsoft.com> wrote:
I need the ability to create a series of individual files via one
Streamwriter (one at a time). Currently I have an application that is
writing out all of the data to one text file. The user has requested the
data is to be broken into individual files (could be from one to many depend
on the volume of data). This could lead to many separate files being
created. How do I write to a new file name using “StreamWriter sr =
File.CreateText(FileName)” ?


You need to flush the old stream writer when you're done with one file
and create a new instance via File.CreateText(string), as you indicate.

An idea: write a loop and name the files in sequence, for example:

---8<---
int fileNumber = 1;
while (!someWork.IsFinished)
{
using (TextWriter writer =
File.CreateText(string.Format("File{0}.txt", fileNumber)))
foreach (string someStuff in someWork)
writer.Write(someStuff);
++fileNumber;
}
--->8---

Without knowing more about what exactly your problem is, I think it is
difficult to help you.

-- Barry
May 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.