473,469 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Opening Notepad file in TextBox

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
21 12934
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
"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
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
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
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
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
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
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
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
Minor addition: Optional

Public Function sReadFile (sFilePath As String, _
Optional tToFailOnErrors As Boolean = False) As String
Nov 20 '05 #11
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
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
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
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
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
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
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
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
Hi Herfried, Cor,

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

Regards,
Fergus
Nov 20 '05 #20
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: PM | last post by:
Has anyone found a way to open a file exclusively where it will fail if the file is already open, i have tried the following _FileStream = new FileStream(@"C:\Data.txt", FileMode.Open,...
3
by: RN Das | last post by:
Hi expert, I have a small web application in C# and ASP.Net and I want to open a file in notepad when clicked on a linked button. Here is hander code when clicked on the link button. It can open...
2
by: Suma | last post by:
Hi To open a notepad file, right now I am using Process.start. But this does not ensure that the file is opened in readonly mode How can I make a notepad file open in Readonly mode I do not...
18
by: mollyf | last post by:
I just installed SQL Server 2005 on my PC (the developer's edition) yesterday. I have some scripts written by one of my coworkers to create some tables and stored procedures in a database that...
2
by: connor_a | last post by:
Hi everyone, I want the user to be able to click a button on my website that creates a new text file, writes to it and then runs notepad to display the text file. How can this be coded on the...
36
by: Don | last post by:
I wrote an app that alerts a user who attempts to open a file that the file is currently in use. It works fine except when the file is opened by Notepad. If a text file is opened, most computers...
5
by: =?Utf-8?B?bG9rZWU=?= | last post by:
Hello, I use windows XP ver.2002, Service pack 2. I am getting a very strange error. I am unable to open any file with extension".ini" If I create any text file, type some text and save it...
1
by: vaniKanabathy | last post by:
I need to call back what i had write in notepad using vb application. I can do it using the coding below but the problem is i want to retrive the value in different textbox but all the values in...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.