Home All Groups Group Topic Archive Search About

Best way to write to textfiles?

Author
11 Dec 2006 10:36 AM
Nettan
Hi

What is the best way to write to a textfile, is it with FileSystemObject or
with StreamWriter?

Thanks  /Nettan

Author
11 Dec 2006 11:00 AM
Phill W.
Nettan wrote:

> What is the best way to write to a textfile, is it with FileSystemObject or
> with StreamWriter?

StreamWriter.  Absolutely.

The FileSystemObject was, at best, a feeble crutch for ASP, which didn't
have any other way of getting into the file system.

The functionality in the System.IO Namespace simply blows the FSO away.

Regards,
    Phill  W.
Author
11 Dec 2006 1:12 PM
Oenone
Phill W. wrote:
>> What is the best way to write to a textfile, is it with
>> FileSystemObject or with StreamWriter?
>
> StreamWriter.  Absolutely.

Or in VB2005, I prefer IO.File.WriteAllText and IO.File.AppendAllText, which
reduces most textfile operations to one line of code.

--

(O)enone
Author
11 Dec 2006 9:25 PM
Herfried K. Wagner [MVP]
"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-k> schrieb:
>> What is the best way to write to a textfile, is it with FileSystemObject
>> or with StreamWriter?
>
> StreamWriter.  Absolutely.
>
> The FileSystemObject was, at best, a feeble crutch for ASP, which didn't
> have any other way of getting into the file system.
>
> The functionality in the System.IO Namespace simply blows the FSO away.

That's true, with the exception that I'd continue to use the FSO in projects
migrated from VB6 until the code gets rewritten.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
12 Dec 2006 6:15 AM
Cor Ligthert [MVP]
Nettan,

The best way is that one which fits you the best. There are no best ones in
this kind of open questions,  all the possibilities are not created for
fun..

Cor

Show quoteHide quote
"Nettan" <Net***@discussions.microsoft.com> schreef in bericht
news:ACCA2908-9A0A-4484-98A5-CE0A6AFEBA6D@microsoft.com...
> Hi
>
> What is the best way to write to a textfile, is it with FileSystemObject
> or
> with StreamWriter?
>
> Thanks  /Nettan
Author
12 Dec 2006 4:37 PM
Garry
Well, can someone point to an article comparing the various methods so that
I can get some serious info.

In VB6, the FSO would, on some computers, activate an anti script defensive
program, (Malicious script detected), so I used to use the most 'basic of
basic' -  'Open for output' type VB methods which were unbelievably fast.

A link to comparative text as per the available methods would be much
appreciated.

Garry



Show quoteHide quote
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
news:OqKkQSbHHHA.4068@TK2MSFTNGP03.phx.gbl...
> Nettan,
>
> The best way is that one which fits you the best. There are no best ones
> in this kind of open questions,  all the possibilities are not created for
> fun..
>
> Cor
>
> "Nettan" <Net***@discussions.microsoft.com> schreef in bericht
> news:ACCA2908-9A0A-4484-98A5-CE0A6AFEBA6D@microsoft.com...
>> Hi
>>
>> What is the best way to write to a textfile, is it with FileSystemObject
>> or
>> with StreamWriter?
>>
>> Thanks  /Nettan
>
>
Author
12 Dec 2006 10:21 PM
RobinS
Here's one method. This is quick. You can also check out
the StreamReader and StreamWriter classes.

This reads a file and splits it into lines using the CrLf characters
that are (hopefully) at the end of each line.

Dim crlfs() as String = {ControlChars.CrLf}
Dim lines() as String = File.ReadAllText("C:\data.txt").Split(crlfs, _
  StringSplitOptions.None)
Dim numOfLines as Integer = lines.Length

If you want to do the opposite, you can do this:

Dim newText as String = String.Join(ControlChars.CrLf, _
  lines, 1, lines.Length - 1)
File.WriteAllText("C:\Data2.txt", newText)

Robin S.
------------------------------------
Show quoteHide quote
"Garry" <garrygrol***@gmail.com> wrote in message
news:uCBAQvgHHHA.4760@TK2MSFTNGP03.phx.gbl...
> Well, can someone point to an article comparing the various methods so
> that I can get some serious info.
>
> In VB6, the FSO would, on some computers, activate an anti script
> defensive program, (Malicious script detected), so I used to use the
> most 'basic of basic' -  'Open for output' type VB methods which were
> unbelievably fast.
>
> A link to comparative text as per the available methods would be much
> appreciated.
>
> Garry
>
>
>
> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message
> news:OqKkQSbHHHA.4068@TK2MSFTNGP03.phx.gbl...
>> Nettan,
>>
>> The best way is that one which fits you the best. There are no best
>> ones in this kind of open questions,  all the possibilities are not
>> created for fun..
>>
>> Cor
>>
>> "Nettan" <Net***@discussions.microsoft.com> schreef in bericht
>> news:ACCA2908-9A0A-4484-98A5-CE0A6AFEBA6D@microsoft.com...
>>> Hi
>>>
>>> What is the best way to write to a textfile, is it with
>>> FileSystemObject or
>>> with StreamWriter?
>>>
>>> Thanks  /Nettan
>>
>>
>
>
Author
13 Dec 2006 5:52 AM
Tom Shelton
On 2006-12-11, Nettan <Net***@discussions.microsoft.com> wrote:
> Hi
>
> What is the best way to write to a textfile, is it with FileSystemObject or
> with StreamWriter?
>
> Thanks  /Nettan

I have to respectfully disagree with Cor on this one.  Using the FSO from .NET
is really a lot of un-needed overhead.

Here are my personal rules for file handling...

Forget the FSO.  You don't need it and it just introduces the extra overhead
of COM interop, on one of those operations that can be one of the big bottle
necks in an applications performance - File IO.

Forget about the VB.NET FileXXX functions.  They are probably slower then the
FSO :)  No, I haven't done any benchmarks to compare the speeds - just playing
a bit.

So, that leaves you with the objects from System.IO.  So, my advice is to get
to know System.IO real well and forget the other file access methods.

--
Tom Shelton
Author
13 Dec 2006 7:51 AM
Cor Ligthert [MVP]
Tom,

I was not particulary looking at those, I have read it as

What is the best way to write to a textfile, is it with FileSystemObject or
with StreamWriter?

The second part was in my opinion less important than the first, in other
words I did not read:

What is the better way to write a textfile, is it with FileSystemObject or
with Stream Writer.

In this context we full agree again.

Probably my way of reading English,

Cor


Show quoteHide quote
"Tom Shelton" <tom_shel***@comcastXXXXXXX.net> schreef in bericht
news:tsednWWaVPeECOLYnZ2dnUVZ_vvinZ2d@comcast.com...
> On 2006-12-11, Nettan <Net***@discussions.microsoft.com> wrote:
>> Hi
>>
>> What is the best way to write to a textfile, is it with FileSystemObject
>> or
>> with StreamWriter?
>>
>> Thanks  /Nettan
>
> I have to respectfully disagree with Cor on this one.  Using the FSO from
> .NET
> is really a lot of un-needed overhead.
>
> Here are my personal rules for file handling...
>
> Forget the FSO.  You don't need it and it just introduces the extra
> overhead
> of COM interop, on one of those operations that can be one of the big
> bottle
> necks in an applications performance - File IO.
>
> Forget about the VB.NET FileXXX functions.  They are probably slower then
> the
> FSO :)  No, I haven't done any benchmarks to compare the speeds - just
> playing
> a bit.
>
> So, that leaves you with the objects from System.IO.  So, my advice is to
> get
> to know System.IO real well and forget the other file access methods.
>
> --
> Tom Shelton