Opening Notepad file in TextBox

  • Thread starter Thread starter Sender
  • Start date Start date
S

Sender

I have two questions:

(1) I just want to check whether a .txt file is empty or not (without
opening it). Like I click on a command button then it should give message
EMPTY if is empty and show message "NON EMPTY" if it is not empty.

(2) What are the steps to open a notepad file (C:\XYZ\abc.txt) in a VB Text
Box.

Thanks
 
Sender said:
I have two questions:

(1) I just want to check whether a .txt file is empty or not
(without opening it). Like I click on a command button then it should
give message EMPTY if is empty and show message "NON EMPTY" if it is
not empty.

An empty file is a file with size = 0?

Use Microsoft.VisualBasic.FileSystem.FileLen.
(2) What are the steps to open a notepad file (C:\XYZ\abc.txt) in a
VB Text Box.

Set the Textbox' multiline property = True. See also:

<F1>
Visual Studio.NET
Visual Basic and Visual C#
Reference
Visual Basic language
Visual Basic Language Tour
-> Processing drives, folders and files
.NET Framework
Programming with .NET Framework
-> Working with I/O
 
I'm new to .Net (just a couple days) so there might be a better way to
do this.

Question: What does "Empty" mean? Is the file empty if it contains one
space? Is it empty if it doesn't exist?

You can check the length of an existing file to see if it's greater
than zero but, by definition, you can't see what;'s inside of it
without opening it.

==== To check the length of a text file ====

Dim FileSize As Long

Try
FileSize = FileLen("C:\SomeFile.Txt")
Catch exc As FileNotFoundException
FileSize = 0
Finally
Select Case FileSize
Case Is > 0
MsgBox("NON EMPTY: " & FileSize.ToString)
Case Else
MsgBox("EMPTY")
End Select
End Try


==== To read a text file: ====

Dim myStreamReader As StreamReader

myStreamReader = File.OpenText("C:\SomeFile.Txt")

Me.txtFileText.Text = myStreamReader.ReadToEnd()

If Not myStreamReader Is Nothing Then
myStreamReader.Close()
End If
 
Hi Sender,

You could use something like the following:
MyForm.TextBox1.Text = sReadFile ("C:\XYZ\abc.txt", False)

Public Module FileUtils
Public Function sReadFile (sFilePath As String, _
tToFailOnErrors As Boolean) As String
If Not File.Exists (sFilePath) Then
Return ""
End If

Dim sFileContents As String = ""
Dim strmFile As TextReader
Try
strmFile = File.OpenText (sFilePath)
sFileContents = strmFile.ReadToEnd

Catch e As Exception
If tToFailOnErrors Then Throw e

Finally
If Not strmFile Is Nothing Then
strmFile.Close()
End If
End Try

Return sFileContents
End Function
End Module

Regards,
Fergus
 
Hi,
I like that everybody has entered his cake.
Here is mine, I thought why do I need a textbox for an empty file.
\\\\
Dim sr As StreamReader = New StreamReader("file")
if not sr is nothing then
Dim line As String
line = sr.ReadLine
If line = Nothing Then
Me.label1 = "Empty"
Else
Me.label1 = "Filled"
End If
else
me.label1="Does not exist"
end if
////
It is just a little cake I know and without creme.
Cor
 
Hello,

Sender said:
(1) I just want to check whether a .txt file is empty or
not (without opening it).

\\\
MsgBox((FileLen("C:\bla.txt") = 0).ToString())
///
(2) What are the steps to open a notepad file (C:\XYZ\abc.txt)
in a VB Text Box.

\\\
Dim s As New System.IO.StreamReader("C:\bla.txt")
Me.TextBox1.Text = s.ReadToEnd()
s.Close()
///
 
Hello,

Cor said:
I like that everybody has entered his cake.
Here is mine, I thought why do I need a textbox for
an empty file.
\\\\
Dim sr As StreamReader = New StreamReader("file")
if not sr is nothing then
Dim line As String
line = sr.ReadLine
If line = Nothing Then
Me.label1 = "Empty"
Else
Me.label1 = "Filled"
End If
else
me.label1="Does not exist"
end if
////

Why not use 'System.IO.File.Exists'?
It is just a little cake I know and without creme.

;-)
 
Herfried,
In my eyes was the question an empty string in a existing .txt file.
The test for nothing I put at the last moment in my code ,
Before that was the code
If (line Is nothing) or (line = nothing) then
I did find this funny code thinking of all the discussions of that, but I
don't know if a txt file can exist with null records in it.

Maybe I test it tomorrow
It is a new streamwriter with a direct close I think .

But I think he adds an empty record.
Cor
 
Minor addition: Optional

Public Function sReadFile (sFilePath As String, _
Optional tToFailOnErrors As Boolean = False) As String
 
Hi Cor,

|| Can a txt file can exist with null records in it?

Yes - I have plenty of zero-length text files. They crop up in C:\Tmp a
lot, and many log files exist but are empty, etc, etc.

Using the function that I gave earlier I would use:

lblFileContents.Text = IIF (sReadFile (sFilePath) = "", _
"Empty", "Not empty")

Regards,
Fergus
 
Fergus,
This is again a discussion about nothing.
Not about nothing ofcours I mean nothing you know nothing if is nothing and
nothing things like nothing.
But I did mean it serious, but the discussion is of course the the same as
your cluster.f*ck that I translated in form check or something.

By the way do you know if you have to overload a cluster form check or can
you just override it with a stack and than make a key event from it?.

(Now you have that anwer too)
Cor
 
Hi Herfried,
That gives a record, I try it tomorrow, there only has to be something in
the directory, without any record than it is Line is nothing, when you open
a notepath you have automatic line=nothing.
I am not anymore in the mood for that.
Cor
 
Howdy Cor,

|| This is again a discussion about nothing.
|| Not about nothing ofcours I mean nothing you know
|| nothing if is nothing and nothing things like nothing.
||
|| But I did mean it serious,

So did I. I was agreeing with you that you have to read the file rather
than simply check its existance.

But I'll never use sSomeString = Nothing. Nor, most probably sSomeString =
String.Empty. What's the point, when "" has been understood for decades?

|| but the discussion is of course the the same as
|| your cluster.f*ck that I translated in form check or something.

It is?

|| By the way do you know if you have to overload a cluster
|| form check or can you just override it with a stack and than
|| make a key event from it?.

Me thinks you jest. ;-). I hope so 'cos otherwise I haven't a clue.

Regards,
Fergus
MVP [Windows Start button, Shutdown dialogue]
 
Hi Fergus,
This is about the serious part of my message.
You are maybe right, but for decades I did found the "" very uggly.
There was that discussion in this newsgroup about nothing (I mean this
serious) and I thought that looks very nice.
But next time I use in this newsgroup again "" it is better to show, because
otherwise people maybe will look first at that and not too the realing
meaning of a sample.

Cor
 
Good morning Cor, :-)

|| There was that discussion in this newsgroup
|| about nothing (I mean this serious)

If you put 'nothing' in quotes, people will know you are not talking about
nothing but about something called 'nothing' ;-)

But it makes for some great sentences when you leave out the quotes and
have to explain nothing ;-))

|| But next time I use in this newsgroup again "" it is better to
|| show, because otherwise people maybe will look first at
|| that and not too the realing meaning of a sample

Good idea.

Regards,
Fergus
MVP [Windows Keyboard, PC Power Switch]
 
Herfried,

Try it, you will be suprissed.

It looks if there is only a name in the directory, but no file.

Or maybe I did something wrong and than I am crying.

Cor
 
Hi Herfried, Cor,

I haven't tested it but what Cor says, is exactly what I'd expect.

Regards,
Fergus
 
Back
Top