writing to a file?

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

How would I write data to a file? For example a .Doc file?

Say I have a form that has textboxes:

txtname, txtaddress, txtphone

then I want to create a file that would be simple and look like this:

FORM INFORMATION:

Name: "contents of txtname"
Address: "contents of txtaddres"
Tel: "contents of txtphone"

I then want to save this file as Jan1620081.doc and store it out
somewhere? How would I do this, for a button click event?

thanks for any input.
 
How would I write data to a file? For example a .Doc file?

Say I have a form that has textboxes:

txtname, txtaddress, txtphone

then I want to create a file that would be simple and look like this:

FORM INFORMATION:

Name: "contents of txtname"
Address: "contents of txtaddres"
Tel: "contents of txtphone"

I then want to save this file as Jan1620081.doc and store it out
somewhere? How would I do this, for a button click event?

thanks for any input.

Look at StreamWriter class:

http://msdn2.microsoft.com/en-us/library/system.io.streamwriter.aspx
 
Jason said:
How would I write data to a file? For example a .Doc file?

Say I have a form that has textboxes:

txtname, txtaddress, txtphone

then I want to create a file that would be simple and look like this:

FORM INFORMATION:

Name: "contents of txtname"
Address: "contents of txtaddres"
Tel: "contents of txtphone"

I then want to save this file as Jan1620081.doc and store it out
somewhere? How would I do this, for a button click event?

For text files: 'System.IO.StreamWriter'.

If the ".doc" file extension refers to a Microsoft Office Word Document
file, then you'll have to use a library which supports this file format.
You can do that by automating Word, for example. Just check out
<URL:http://msdn2.microsoft.com/office/>.
 
How would I write data to a file? For example a .Doc file?

Say I have a form that has textboxes:

txtname, txtaddress, txtphone

then I want to create a file that would be simple and look like this:

FORM INFORMATION:

Name: "contents of txtname"
Address: "contents of txtaddres"
Tel: "contents of txtphone"

I then want to save this file as Jan1620081.doc and store it out
somewhere? How would I do this, for a button click event?

thanks for any input.

Code sample would be for example for txtname textbox:

Using myStreamWriter As IO.StreamWriter = New
System.IO.StreamWriter(<filepath>)
myStreamWriter.Write(txtname.Text)
myStreamWriter.Flush()
End Using

If you want to write a line into file, use "writeline" function.
 
Herfried said:
For text files: 'System.IO.StreamWriter'.

I'm curious: except for the possibility that the OP may be using VS2003 or
earlier (which I think less and less likely these days), I'm puzzled as to
why all the responses to this question have suggested using StreamWriter,
when the IO.File.WriteAllText() function seems to me to be much easier and
simpler to use..?

The WriteAllText, WriteAllBytes and WriteAllLines functions (and their
corresponding Read functions) are nuggets of gold in my opinion, but no one
ever seems to mention them. Am I overlooking something?
 
I'm curious: except for the possibility that the OP may be using VS2003 or
earlier (which I think less and less likely these days), I'm puzzled as to
why all the responses to this question have suggested using StreamWriter,
when the IO.File.WriteAllText() function seems to me to be much easier and
simpler to use..?

The WriteAllText, WriteAllBytes and WriteAllLines functions (and their
corresponding Read functions) are nuggets of gold in my opinion, but no one
ever seems to mention them. Am I overlooking something?

One more:

My.Computer.FileSystem provides such functions like read/write
operations to text files.
 
Jason said:
How would I write data to a file?

Using a StreamWriter.
For example a .Doc file?

Ah; now that's a whole different can of worms.
Say I have a form that has textboxes:
txtname, txtaddress, txtphone
then I want to create a file that would be simple and look like this:

FORM INFORMATION:

Name: "contents of txtname"
Address: "contents of txtaddres"
Tel: "contents of txtphone"

I then want to save this file as Jan1620081.doc and store it out
somewhere? How would I do this, for a button click event?

Leaving out the ".doc" bit for a while:

Dim sw as new StreamWriter( "<path>\Jan1620081.doc", ...
Dim sLinePattern as String = "{0}: {1}"

sw.WriteLine( String.Format( sLinePattern _
, "Name", txtName.Text ) )
sw.WriteLine( String.Format( sLinePattern _
, "Address", txtAddress.Text ) )
.. . .
sw.Close()


Now, about that .doc thing.

The above code /will not work/ for .doc files. They are a proprietary,
binary format with all sorts of weird and wonderful (and completely
undocumented) stuff in them.

You /could/ muck about automating the MS Word application and put the
text in that way, but it's probably not worth the effort. I would
suggest that you're probably better off writing your output as RTF or
HTML, which Word is capable of reading.

(If you're feeling /brave/, read on...)

' Writing output as RTF
Dim sw as new StreamWriter( "<path>\Jan1620081.rtf", ...
Dim sLinePattern as String = "{\pard {0}: {1}\par}"

sw.Write( "{\rtf1\ansi\ansicpg1252 \fs24 " )

sw.WriteLine( String.Format( sLinePattern _
, "Name", txtName.text ))
sw.WriteLine( String.Format( sLinePattern _
, "Address", txtAddress.text ))
.. . .
sw.Write( "}" )
sw.Close()

HTH,
Phill W.
 
How would I write data to a file? For example a .Doc file?

Say I have a form that has textboxes:

txtname, txtaddress, txtphone

then I want to create a file that would be simple and look like this:

FORM INFORMATION:

Name: "contents of txtname"
Address: "contents of txtaddres"
Tel: "contents of txtphone"

I then want to save this file as Jan1620081.doc and store it out
somewhere? How would I do this, for a button click event?

thanks for any input.

My.Computer.FileSystem.WriteAllText("C:\TestFolder1\test.txt", _
"This is new text to be added.",True)
 
Back
Top