473,545 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ 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 12944
Use the System.io.FileI nfo class.

you'll find much reading on that. =)

-CJ
"Sender" <us**@domain.co m> wrote in message
news:ui******** ********@TK2MSF TNGP12.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.co m> 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.Visua lBasic.FileSyst em.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.co m> wrote in message
news:ui******** ********@TK2MSF TNGP12.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:\Som eFile.Txt")
Catch exc As FileNotFoundExc eption
FileSize = 0
Finally
Select Case FileSize
Case Is > 0
MsgBox("NON EMPTY: " & FileSize.ToStri ng)
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.co m> 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.tx t", 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.ReadTo End

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("f ile")
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.co m> 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.Strea mReader("C:\bla .txt")
Me.TextBox1.Tex t = 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("f ile")
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
714
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, FileAccess.ReadWrite, FileShare.None); which does not prevent me from opening the file even if another user has the file open ie Notepad, it will prevent me...
3
4842
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 a process of notepad on the server , but does not open the file in notepad. What am I doing wrong? can you show me how to do it please? What I am...
2
11148
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 want to change its attributes to Readonly I do not want to open it in browser or in any other editor except notepad I do not want to use MFCAppWizard...
18
6698
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 I've already created on my computer. Whenever I go to open the script file (whose icon shows that it's a SQL Server Query File and I've got the file...
2
1851
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 client side? Works fine but only on server side at the moment. ;-) Thanks for any assistance.
36
5348
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 are configured to use Notepad to open the file by default and if they are configured to use Notepad by default I want it to remain that way rather...
5
3650
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 with extension ".ini" and try to open it using notepad (or by double clicking), it gives following messege:
1
3152
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 notepad appear in 1 textbox.Like in notepad i got values like f g In textbox the values appear like Text1 = fg ...
0
7478
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3466
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.