Opening up a text file ?

  • Thread starter Thread starter Keith(Southend)
  • Start date Start date
K

Keith(Southend)

Starting from basics here I'm afraid, but after succesfully carrying out
the following code:
My.Computer.Audio.Play("c:\WINDOWS\media\Windows XP Startup.wav")

I thought I'd see if I could actually get to a text file, the first step
of what I will need to do in my Project. The IntelliSense suggest a
couple of things and I think it's the IO thing I need. Just wondered if
someone could point me in the right direction here.

My.Computer.FileSystem.GetFiles(FileIO.FileSystem.GetFiles("C:\SynopData\Formatted\2009071712xx.txt)
Where am I going wrong?

I'm sure once I have a few solutions cracked things will become more
obvious.

Many thanks
 
Keith(Southend) said:
Starting from basics here I'm afraid, but after succesfully carrying out
the following code:
My.Computer.Audio.Play("c:\WINDOWS\media\Windows XP Startup.wav")

I thought I'd see if I could actually get to a text file, the first step
of what I will need to do in my Project. The IntelliSense suggest a couple
of things and I think it's the IO thing I need. Just wondered if someone
could point me in the right direction here.

Opening a file in its associated program:
'System.Diagnostics.Process.Start(<file name>)'.

Opening a file to read its contents: 's = System.IO.File.ReadAllText(<file
name>)'.
 
Herfried said:
Opening a file in its associated program:
'System.Diagnostics.Process.Start(<file name>)'.

Opening a file to read its contents: 's =
System.IO.File.ReadAllText(<file name>)'.

I got the first option working ok, but not the second, what does the 's
= part mean? Hovering over it it say's Name 's' not declared, normally
this means you have to add the declaration at the start.

Just mixing tutorials with some functions I know I'm going to need.

Thanks
 
James said:
Try:

Dim s As String = System.IO.File.ReadAllText("<filename>")

Thanks James, I didn't notice anything happening, no errors etc, but I
guess the programme read the file, but until I tell it to do something
with it I wouldn't see anything.

Much appreciated, I'm slowly getting my head around the whole language,
it's quite alien to me, but I'm not giving up :-)

Best regards
 
Keith(Southend) said:
Thanks James, I didn't notice anything happening, no errors etc, but I
guess the programme read the file, but until I tell it to do something
with it I wouldn't see anything.

You could assign the value returned by 'ReadAllText' to a textbox control's
'Text' property:

\\\
Imports System.IO
....
Me.TextBox1.Text = File.ReadAllText(<file name>)
///
 
Herfried said:
You could assign the value returned by 'ReadAllText' to a textbox
control's 'Text' property:

\\\
Imports System.IO
...
Me.TextBox1.Text = File.ReadAllText(<file name>)
///

Yep, that worked :-)

Public Class TestProjectForm

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Me.tbMessage.Text = "Hello World"
MessageBox.Show("Hello Again")
For i As Integer = 1 To 2
Me.TextBox2.Text += i.ToString + ","
Dim Foo As Integer
Dim bar As Double
My.Computer.Audio.Play("c:\WINDOWS\media\Windows XP
Startup.wav")
Dim s As String =
System.IO.File.ReadAllText("c:\SynopData\2009071718.txt")
Me.TextBox1.Text =
IO.File.ReadAllText("c:\SynopData\2009071718.txt")
Next
End Sub

End Class

Just working my way through 'Build a Program Now! by Patrice Pelland.
Now I've tinkered around at that section I'll move on now.

Thanks again for your help.
 
Keith(Southend) said:
System.IO.File.ReadAllText("c:\SynopData\2009071718.txt")
Me.TextBox1.Text =
IO.File.ReadAllText("c:\SynopData\2009071718.txt")
Next

Actually I changed it to a RichTextBox1 as the text file is quite a few
lines. But I am beginning to get to see how this works now. I have a
couple of similar projects someone wrote for me back in 2002,
unfortunately I can't get to the code, would be really useful if I
could. However, I reckon they were built on VB or something very similar.
 
Back
Top