Best way to write to textfiles?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

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

Thanks /Nettan
 
Nettan said:
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.
 
Phill said:
StreamWriter. Absolutely.

Or in VB2005, I prefer IO.File.WriteAllText and IO.File.AppendAllText, which
reduces most textfile operations to one line of code.
 
Phill W. said:
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.
 
yeah.. why should you rewrite it at all?

keep it in vb6 until MS decided the fate of VB

I heard it's all changing again in another year or so

-Aaron
 
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
 
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
 
is this a VB6 problem

Malicious script detected


or is it some C++ fag that doesn't respect VB6 that put a crappy
security mechanism on it?

kinda like the triple prompts when opening MS Access.. or the prompts
to send mail automagically using the damn outlook object model?

it's all because some C++ fag was in control and crippled VB.

And I've never seen this message.. except from like Norton / Symantec
and everyone knows that they eat a dick right?

-Aaron
 
and do you really think that open for output is the fastest?
you got any benchmarks to share?

I thought that the textstream was supposed to be faster

in a perfect world; everything would be a performance decision.

is X or Y faster; use X.

I think that performance-- ease of development included-- is a limiting
factor and end user performance is fast enough in almost anything that
we do.
 
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.
 
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,

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
 
Tom

if it ain't broke don't fix it

I just wish those dipshits in Redmond had some real world experience
and decided not to change EVERYTHING

it's not like they changed 1 or 2 or 10 things.. every single ****ing
language just HAD TO CHANGE DIDNT IT?

those ****ing dipshits in Redmond should just STFU and make our
existing libraries _FASTER_

-Aaron
 
Back
Top