By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

Opening Notepad file in TextBox

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
Use the System.io.FileInfo class.

you'll find much reading on that. =)

-CJ
"Sender" <us**@domain.com> wrote in message
news:ui****************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #2

P: n/a
"Sender" <us**@domain.com> schrieb
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
--
Armin

- The tree representing the table of contents has been translated from
localized (German) version. Excuse slight deviations.

Nov 20 '05 #3

P: n/a
Read the file in as a stream and assign the contents of the stream to the
text box. You'll have to convert it first to an ASCII Representation..

check out this link..it should get you through it.
http://www-tcsn.experts-exchange.com..._20670042.html
HTH,

Bill
"Sender" <us**@domain.com> wrote in message
news:ui****************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #4

P: n/a
Don

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

On Thu, 18 Sep 2003 11:50:37 -0700, "Sender" <us**@domain.com> wrote:
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


Nov 20 '05 #5

P: n/a
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
Nov 20 '05 #6

P: n/a
Cor
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
Nov 20 '05 #7

P: n/a
Hello,

"Sender" <us**@domain.com> schrieb:
(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()
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #8

P: n/a
Hello,

"Cor" <no*@non.com> schrieb:
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 K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #9

P: n/a
Cor
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
Nov 20 '05 #10

P: n/a
Minor addition: Optional

Public Function sReadFile (sFilePath As String, _
Optional tToFailOnErrors As Boolean = False) As String
Nov 20 '05 #11

P: n/a
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
Nov 20 '05 #12

P: n/a
Hello,
"Cor" <no*@non.com> schrieb:
don't know if a txt file can exist with null records in it.


Just create an "empty" text file using notepad.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #13

P: n/a
Cor
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
Nov 20 '05 #14

P: n/a
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

Nov 20 '05 #15

P: n/a
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]

Nov 20 '05 #16

P: n/a
Cor
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
Nov 20 '05 #17

P: n/a
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]
Nov 20 '05 #18

P: n/a
Cor
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
Nov 20 '05 #19

P: n/a
Hi Herfried, Cor,

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

Regards,
Fergus
Nov 20 '05 #20

P: n/a
Hello,

"Cor" <no*@non.com> schrieb:
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.


The result is exactly what I would expect.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #21

P: n/a
Hello,

"Cor" <no*@non.com> schrieb:
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.


The result is exactly what I would expect.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.