How to write a text file?

C

Charlie Kent

More specifically an INI. I've been using VBScript and batches for
years as an administrator and decided last week to go buy Visual
Studio 2005 (in a discount bin) just to learn something new. I
figured I would start with something other than hello world.

So far I've modeled what I want to do in a batch file and now I'm
taking the steps to "translate" my batch (inigen.cmd) into VB.Net
(inigen.vb). The inigen is really just me farting around with code
snippets and mutlilating them. So far I haven't had much success.
Then again I've just started puttering around with it for the first
time today. ;)

Any easy ways to write to a text file with vb.net.

On a side note, any good books/websites I should keep an eye out for.
Typing vb.net in google brings a lot back and I'd rather lower the
noise to signal ratio.

Cheers!
Charlie.K


==========inigen.cmd========
@echo off
echo //>>tmpfile.tmp
echo // GENERAL>>tmpfile.tmp
echo // show language show language selection screen (true or
false)>>tmpfile.tmp
echo // background set bitmap for background on screen>>tmpfile.tmp
echo // product logo set bitmap for product logo on
screen>>tmpfile.tmp
echo // mheight set size of height of message window>>tmpfile.tmp
echo // mborder set size of border of message window>>tmpfile.tmp
echo // mfontsize set font size of text in message window>>tmpfile.tmp
echo // mfontbold set text in message window to bold (true or
false)>>tmpfile.tmp
echo // mfontitalics set text in message window to italics (true or
false)>>tmpfile.tmp
echo // mforecolor color of text in message window (use color chart
below)>>tmpfile.tmp
echo // mfontjustify justify text left(0), right(1),
center(2)>>tmpfile.tmp
echo.>>tmpfile.tmp
echo.>>tmpfile.tmp
echo // plang key = UI Language>>tmpfile.tmp
echo // Eng=\setup.exe>>tmpfile.tmp
echo.>>tmpfile.tmp
echo // LANGUAGE>>tmpfile.tmp
echo // picture - if blank then no picture just solid
background>>tmpfile.tmp
echo // border - if 0 then no border around picture>>tmpfile.tmp
echo // bordercolor - color of border or background if no picture (see
color table at end)>>tmpfile.tmp
echo //>>tmpfile.tmp
echo //>>tmpfile.tmp
echo.>>tmpfile.tmp
echo // COLOR TABLE>>tmpfile.tmp
echo // Grey 16777215 (White) Green 12648384>>tmpfile.tmp
echo // 14737632 8454016>>tmpfile.tmp
echo // 12632256 65280>>tmpfile.tmp
echo // 8421504 49152>>tmpfile.tmp
echo // 4210752 32768>>tmpfile.tmp
echo // 0 (Black) 16384>>tmpfile.tmp
echo // Red 12632319 (light) Light Blue 16777152>>tmpfile.tmp
echo // 8421631 16777088>>tmpfile.tmp
echo // 255 16776960>>tmpfile.tmp
echo // 192 12632064>>tmpfile.tmp
echo // 128 8421376>>tmpfile.tmp
echo // 64 (dark) 4210688>>tmpfile.tmp
echo // Orange 12640511 Blue 16761024>>tmpfile.tmp
echo // 8438015 16744576>>tmpfile.tmp
echo // 33023 16711680>>tmpfile.tmp
echo // 16576 12582912>>tmpfile.tmp
echo // 16512 8388608>>tmpfile.tmp
echo // 4210816 4194304>>tmpfile.tmp
echo // Yellow 12648447 Purple 16761087>>tmpfile.tmp
echo // 8454143 16744703>>tmpfile.tmp
echo // 65535 16711935>>tmpfile.tmp
echo // 49344 12583104>>tmpfile.tmp
echo // 32896 8388736>>tmpfile.tmp
echo // 16448 4194368>>tmpfile.tmp
echo.>>tmpfile.tmp
echo [GENERAL]>>tmpfile.tmp
echo prior install=true>>tmpfile.tmp
echo show language=false>>tmpfile.tmp
echo background=autorun.bmp>>tmpfile.tmp
echo product logo=>>tmpfile.tmp
echo mheight=1350>>tmpfile.tmp
echo mborder=200>>tmpfile.tmp
echo mfontsize=10>>tmpfile.tmp
echo mfontbold=true>>tmpfile.tmp
echo mfontitalics=false>>tmpfile.tmp
echo mforecolor=4210688>>tmpfile.tmp
echo mfontjustify=2>>tmpfile.tmp
echo.>>tmpfile.tmp
echo [PRIOR]>>tmpfile.tmp
echo AppTitle = Kit or something>>tmpfile.tmp
echo plang key = UI Language>>tmpfile.tmp
echo Eng=\setup.exe>>tmpfile.tmp
echo Fre=\setup.exe>>tmpfile.tmp
echo.>>tmpfile.tmp
echo [LANGUAGE]>>tmpfile.tmp
echo numlang=6>>tmpfile.tmp
echo lang1=&English>>tmpfile.tmp
echo lang2=&Francais>>tmpfile.tmp
echo mess1=Select English as your primary language.>>tmpfile.tmp
echo mess2=Choisissez le français en tant que votre langage
primaire.>>tmpfile.tmp
echo.>>tmpfile.tmp
echo [LANG0] //Default (when language selection is not
displayed)>>tmpfile.tmp
echo numbutton=8>>tmpfile.tmp
echo exit=E&xit>>tmpfile.tmp
echo back=&Back>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button1=Before Starting....>>tmpfile.tmp
echo mess1=Read this before continuing with the Kit >>tmpfile.tmp
echo type1=1>>tmpfile.tmp
echo active1=1>>tmpfile.tmp
echo action1=\Documentation\Kit\beforestart.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button2=Release Notes>>tmpfile.tmp
echo mess2=Information on the contents of this Kit >>tmpfile.tmp
echo type2=1>>tmpfile.tmp
echo active2=1>>tmpfile.tmp
echo action2=\Documentation\Kit\releasenotes.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button3=Documentation>>tmpfile.tmp
echo mess3=Support documents for this install kit>>tmpfile.tmp
echo type3=1>>tmpfile.tmp
echo active3=1>>tmpfile.tmp
echo action3=\Documentation\Product>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button4=Installation>>tmpfile.tmp
echo mess4=Install Release>>tmpfile.tmp
echo type4=1>>tmpfile.tmp
echo active4=1>>tmpfile.tmp
echo action4=\Documentation\Kit\installplaceholder.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button5=Contact Info>>tmpfile.tmp
echo mess5= Contact information>>tmpfile.tmp
echo type5=1>>tmpfile.tmp
echo active5=1>>tmpfile.tmp
echo action5=\Documentation\Kit\contactus.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button6=Version>>tmpfile.tmp
echo mess6= Package and Kit Information>>tmpfile.tmp
echo type6=1>>tmpfile.tmp
echo active6=1>>tmpfile.tmp
echo action6=\Documentation\Kit\version.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button7=FAQ>>tmpfile.tmp
echo mess7=Frequently asked questions>>tmpfile.tmp
echo type7=1>>tmpfile.tmp
echo active7=1>>tmpfile.tmp
echo action7=\Documentation\Kit\howto\howto.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button8=Support Tools>>tmpfile.tmp
echo mess8=Support Tools>>tmpfile.tmp
echo type8=1>>tmpfile.tmp
echo active8=1>>tmpfile.tmp
echo action8=\Documentation\Support>>tmpfile.tmp
:: ^ Add space here if needed.
copy /b tmpfile.tmp+existing.txt>nul
del existing.txt
ren tmpfile.tmp autorun.ini
===========eof=====================

===========inigen.vb=================
Imports System.IO
Imports System.Text


Public Class inigencreator

Sub WriteToFile()

Dim strToWrite As String = "The String To Write"
Dim strFilename As String = "C:\Foo.txt"
Dim fsStream As System.IO


If Not File.Exists(strFilename) Then
fsStream = File.CreateText("C:\File.txt")
Else
fsStream = File.OpenText("C:\File.txt")
End If


fsStream.WriteLine(strToWrite)
fsStream.Close()
End Sub

End Class
=============eof===============
 
Z

zacks

More specifically an INI.  I've been using VBScript and batches for
years as an administrator and decided last week to go buy Visual
Studio 2005 (in a discount bin) just to learn something new.  I
figured I would start with something other than hello world.

Since you are looking into doing something new then you need to get
away from INI files and move to XML files. Look into DOM and
XMLSerialization. They are the two primary ways of reading and writing
XML files with .NET.

If you really want to still use an INI file, I would recommend looking
into the GetPrivateProfileString and SetPrivateProfileStirng APIs.
 
E

eBob.com

I would have started with Hello World, but to each his own. But be aware
that the VS IDE can take a little bit of getting used to.

A nice thing about the VS IDE is the built in Help. I just did a search for
"File i/o" and selected "Basic File I/O" from the hits and am looking at a
nice discussion of, well ..., basic file I/O. (There are many things I
don't care for about the Help facility but it does contain a wealth of
information.)

The SAMS books lack depth, but if your programming experience is really
limited one of their books might be a good place to start.

Here's some code which I abstracted from a working program of mine.


Dim sw As StreamWriter 'for the output history file
Dim hisline As String 'line to be written to the
history file

sw = New StreamWriter(globalinf.ExcelFileName + ".his") 'for the
output history file

sw.WriteLine(hisline)

sw.Close()

To use it you will also need an "Imports" statement ...

Imports System.IO

I'd recommend always using these two statements as the first two statements
in your source file:

Option Explicit On
Option Strict On

These two statements tell the compiler to catch your dumb errors at compile
time rather than allowing them to cause really confusing errors at run time.
(Although you will still experience really confusing errors at run time.)

Good Luck, Bob


More specifically an INI. I've been using VBScript and batches for
years as an administrator and decided last week to go buy Visual
Studio 2005 (in a discount bin) just to learn something new. I
figured I would start with something other than hello world.

So far I've modeled what I want to do in a batch file and now I'm
taking the steps to "translate" my batch (inigen.cmd) into VB.Net
(inigen.vb). The inigen is really just me farting around with code
snippets and mutlilating them. So far I haven't had much success.
Then again I've just started puttering around with it for the first
time today. ;)

Any easy ways to write to a text file with vb.net.

On a side note, any good books/websites I should keep an eye out for.
Typing vb.net in google brings a lot back and I'd rather lower the
noise to signal ratio.

Cheers!
Charlie.K


==========inigen.cmd========
@echo off
echo //>>tmpfile.tmp
echo // GENERAL>>tmpfile.tmp
echo // show language show language selection screen (true or
false)>>tmpfile.tmp
echo // background set bitmap for background on screen>>tmpfile.tmp
echo // product logo set bitmap for product logo on
screen>>tmpfile.tmp
echo // mheight set size of height of message window>>tmpfile.tmp
echo // mborder set size of border of message window>>tmpfile.tmp
echo // mfontsize set font size of text in message window>>tmpfile.tmp
echo // mfontbold set text in message window to bold (true or
false)>>tmpfile.tmp
echo // mfontitalics set text in message window to italics (true or
false)>>tmpfile.tmp
echo // mforecolor color of text in message window (use color chart
below)>>tmpfile.tmp
echo // mfontjustify justify text left(0), right(1),
center(2)>>tmpfile.tmp
echo.>>tmpfile.tmp
echo.>>tmpfile.tmp
echo // plang key = UI Language>>tmpfile.tmp
echo // Eng=\setup.exe>>tmpfile.tmp
echo.>>tmpfile.tmp
echo // LANGUAGE>>tmpfile.tmp
echo // picture - if blank then no picture just solid
background>>tmpfile.tmp
echo // border - if 0 then no border around picture>>tmpfile.tmp
echo // bordercolor - color of border or background if no picture (see
color table at end)>>tmpfile.tmp
echo //>>tmpfile.tmp
echo //>>tmpfile.tmp
echo.>>tmpfile.tmp
echo // COLOR TABLE>>tmpfile.tmp
echo // Grey 16777215 (White) Green 12648384>>tmpfile.tmp
echo // 14737632 8454016>>tmpfile.tmp
echo // 12632256 65280>>tmpfile.tmp
echo // 8421504 49152>>tmpfile.tmp
echo // 4210752 32768>>tmpfile.tmp
echo // 0 (Black) 16384>>tmpfile.tmp
echo // Red 12632319 (light) Light Blue 16777152>>tmpfile.tmp
echo // 8421631 16777088>>tmpfile.tmp
echo // 255 16776960>>tmpfile.tmp
echo // 192 12632064>>tmpfile.tmp
echo // 128 8421376>>tmpfile.tmp
echo // 64 (dark) 4210688>>tmpfile.tmp
echo // Orange 12640511 Blue 16761024>>tmpfile.tmp
echo // 8438015 16744576>>tmpfile.tmp
echo // 33023 16711680>>tmpfile.tmp
echo // 16576 12582912>>tmpfile.tmp
echo // 16512 8388608>>tmpfile.tmp
echo // 4210816 4194304>>tmpfile.tmp
echo // Yellow 12648447 Purple 16761087>>tmpfile.tmp
echo // 8454143 16744703>>tmpfile.tmp
echo // 65535 16711935>>tmpfile.tmp
echo // 49344 12583104>>tmpfile.tmp
echo // 32896 8388736>>tmpfile.tmp
echo // 16448 4194368>>tmpfile.tmp
echo.>>tmpfile.tmp
echo [GENERAL]>>tmpfile.tmp
echo prior install=true>>tmpfile.tmp
echo show language=false>>tmpfile.tmp
echo background=autorun.bmp>>tmpfile.tmp
echo product logo=>>tmpfile.tmp
echo mheight=1350>>tmpfile.tmp
echo mborder=200>>tmpfile.tmp
echo mfontsize=10>>tmpfile.tmp
echo mfontbold=true>>tmpfile.tmp
echo mfontitalics=false>>tmpfile.tmp
echo mforecolor=4210688>>tmpfile.tmp
echo mfontjustify=2>>tmpfile.tmp
echo.>>tmpfile.tmp
echo [PRIOR]>>tmpfile.tmp
echo AppTitle = Kit or something>>tmpfile.tmp
echo plang key = UI Language>>tmpfile.tmp
echo Eng=\setup.exe>>tmpfile.tmp
echo Fre=\setup.exe>>tmpfile.tmp
echo.>>tmpfile.tmp
echo [LANGUAGE]>>tmpfile.tmp
echo numlang=6>>tmpfile.tmp
echo lang1=&English>>tmpfile.tmp
echo lang2=&Francais>>tmpfile.tmp
echo mess1=Select English as your primary language.>>tmpfile.tmp
echo mess2=Choisissez le français en tant que votre langage
primaire.>>tmpfile.tmp
echo.>>tmpfile.tmp
echo [LANG0] //Default (when language selection is not
displayed)>>tmpfile.tmp
echo numbutton=8>>tmpfile.tmp
echo exit=E&xit>>tmpfile.tmp
echo back=&Back>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button1=Before Starting....>>tmpfile.tmp
echo mess1=Read this before continuing with the Kit >>tmpfile.tmp
echo type1=1>>tmpfile.tmp
echo active1=1>>tmpfile.tmp
echo action1=\Documentation\Kit\beforestart.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button2=Release Notes>>tmpfile.tmp
echo mess2=Information on the contents of this Kit >>tmpfile.tmp
echo type2=1>>tmpfile.tmp
echo active2=1>>tmpfile.tmp
echo action2=\Documentation\Kit\releasenotes.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button3=Documentation>>tmpfile.tmp
echo mess3=Support documents for this install kit>>tmpfile.tmp
echo type3=1>>tmpfile.tmp
echo active3=1>>tmpfile.tmp
echo action3=\Documentation\Product>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button4=Installation>>tmpfile.tmp
echo mess4=Install Release>>tmpfile.tmp
echo type4=1>>tmpfile.tmp
echo active4=1>>tmpfile.tmp
echo action4=\Documentation\Kit\installplaceholder.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button5=Contact Info>>tmpfile.tmp
echo mess5= Contact information>>tmpfile.tmp
echo type5=1>>tmpfile.tmp
echo active5=1>>tmpfile.tmp
echo action5=\Documentation\Kit\contactus.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button6=Version>>tmpfile.tmp
echo mess6= Package and Kit Information>>tmpfile.tmp
echo type6=1>>tmpfile.tmp
echo active6=1>>tmpfile.tmp
echo action6=\Documentation\Kit\version.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button7=FAQ>>tmpfile.tmp
echo mess7=Frequently asked questions>>tmpfile.tmp
echo type7=1>>tmpfile.tmp
echo active7=1>>tmpfile.tmp
echo action7=\Documentation\Kit\howto\howto.html>>tmpfile.tmp
echo.>>tmpfile.tmp
echo button8=Support Tools>>tmpfile.tmp
echo mess8=Support Tools>>tmpfile.tmp
echo type8=1>>tmpfile.tmp
echo active8=1>>tmpfile.tmp
echo action8=\Documentation\Support>>tmpfile.tmp
:: ^ Add space here if needed.
copy /b tmpfile.tmp+existing.txt>nul
del existing.txt
ren tmpfile.tmp autorun.ini
===========eof=====================

===========inigen.vb=================
Imports System.IO
Imports System.Text


Public Class inigencreator

Sub WriteToFile()

Dim strToWrite As String = "The String To Write"
Dim strFilename As String = "C:\Foo.txt"
Dim fsStream As System.IO


If Not File.Exists(strFilename) Then
fsStream = File.CreateText("C:\File.txt")
Else
fsStream = File.OpenText("C:\File.txt")
End If


fsStream.WriteLine(strToWrite)
fsStream.Close()
End Sub

End Class
=============eof===============
 
C

Charlie Kent

Thanks eBob

I'm looking at what i wrote and figured out that I should just change
the line I want to instead of just creating the file from scratch. So
poking around I ended up finding a Search and replace piece and i'm
using that. I'm not sure though if I should keep it as a class or do
I build it right into the form? SO far I'm getting pretty confused in
the UI and how to create something in Visual Studio. Although I
prepared for general confusion when I bought it, I didn't realise how
extremely different from batch/wsh/VBScript/Qbasic/Visual Basic it
would be. I'm looking around for a course now...

===========================
'
' sr = search and replace
'
Dim sr As StreamReader = New StreamReader( "autorun.ini" )
Dim fileText As String = sr.ReadToEnd()
Dim i As Integer = fileText.IndexOf("action4=\Documentation\Kit
\installplaceholder.html")
fileText = fileText.Remove(i, 1)
fileText = fileText.Insert(i,"exelocation_TextChanged")
sr.Close()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top