Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> VB.NET: Howto Add And Delete Files, Just looking for useful code
Axis
post May 17 2005, 03:33 PM
Post #1


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 7
Joined: 5-May 05
From: USA
Member No.: 4,756



I am horrible at dealing with files in VB. I was wondering if anyone could give me some general code for a program I'm working on.

I need to be able to add to files, display them, and delete them. I have use code out of a book, but it never works very well. Any suggestions?
Go to the top of the page
 
+Quote Post
miCRoSCoPiC^eaRt...
post May 17 2005, 04:48 PM
Post #2


PsYcheDeLiC dR3aMeR
Group Icon

Group: Admin
Posts: 2,242
Joined: 29-January 05
From: Nakorn Chaisri, Thailand
Member No.: 2,411



Hi Axis,
    I don't know what code you tried out, but the file manipulation routines in VB.NET are extremly simple... To read a text file, all you need to do is create a RichTextBox control on your form, and load the file into it using the StreamReader class. Here's a short example, assuming rtfBox = your rich text box and sr = your stream reader

CODE
Imports System.IO
...
....
'Just a var to hold your filename
Dim FilePath As String = "c:\somefile.txt"

'Define a stream reader and link it to your file
Dim sr As StreamReader = File.OpenText (FilePath)

'Read the file to the end and load it in your rtf box
rtfBox.Text = sr.ReadToEnd


That's it - this will dump the contents of your whole file into the richtextbox.
To save it, you'll have to use a similar mechanism involving a StreamWriter class - you'll have to pass the contents of your rtfbox through the streamwriter into a file. Just look up the syntax of streamwriter in the MSDN libraries.

Also for creating Blank files and deleting them, you can simply use the members of the File class - e.g. File.Exists, File.CreateNew, File.Delete etc.

If all else fails, post here - and I'll send you a working example of a small NotePad clone that I had created just for fun's sake..It's got all the text-file reading/writing routines that you might need.

Regards,
m^e
Go to the top of the page
 
+Quote Post
iGuest
post Apr 11 2008, 01:35 AM
Post #3


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



Replying to miCRoSCoPiC^eaRthLinG

You can check out the FileCleanup tool at http://www.Aspdotnetsoftware.Com/products/FileCleanup/default.Aspx

There are a lot of free, open source vb.Net applications from those guys. The code is very clean, too.

-reply by John
Go to the top of the page
 
+Quote Post
iGuest
post Apr 27 2008, 05:26 PM
Post #4


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 0
Joined: 1-November 07
Member No.: 25,869



C# Vs. VB.NET
VB.NET: Howto Add And Delete Files

C# is MUCH better than Visual Basic. They both are .Net langs, but C# is much more powerful and interesting.
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Apr 28 2008, 03:29 PM
Post #5


Premium Member
Group Icon

Group: [HOSTED]
Posts: 392
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322



I would have to disagree with you on that one. C# and VB .NET run on the same framework and provide the same functionality. VB .NET code is easier to understand to a beginner as its syntax and keyword are much like english. C# on the other hand is like the traditional C/C++ and has a fan franchise of its own. Both of these languages have their advantages and disadvntages. But at the end of the day, they are both equal in terms of power.
Go to the top of the page
 
+Quote Post
Jeigh
post Apr 28 2008, 08:18 PM
Post #6


Whitest Black Mage
Group Icon

Group: [MODERATOR]
Posts: 1,352
Joined: 20-May 05
From: NB, Canada
Member No.: 5,281



Well that's not entirely accurate since. As you said, they both work on the same framework but they also both BUILD upon that same framework. Now both share probably 90+% of their functionality but there are certain features available in some .NET languages that others do not have. This is why they all exist wink.gif In relation to file I/O this won't matter though heh.

When you are working with .net (or any language, but .net one's are quickest for this) it is VERY simple to google for example code blocks for basic functionality usage such as file I/O so in the future it might be more efficient for you to simply do that.
Go to the top of the page
 
+Quote Post
tansqrx
post Apr 28 2008, 10:26 PM
Post #7


Super Member
Group Icon

Group: [HOSTED]
Posts: 533
Joined: 25-April 05
Member No.: 4,374



Hope you don’t mind but I am going to reply to the original post wink.gif There are many ways in which you can manipulate files and you need to be more specific in what you want to do. From what I gather from your post you want to manipulate the files themselves and not the contents. From my experience the best way to do that is with the System.IO.FileInfo class. First you should declare the object.

CODE
Dim fiFile As New System.IO.FileInfo("c:\test.txt")


If for some reason the file does not exist then you can create it.

CODE
fiFile.Create()


You can also copy it to another location.

CODE
fiFile.CopyTo("c:\testCopy.txt")


You can’t actually read or write the contents of the file with a FileInfo object but it is easy to create a Stream.

CODE
Dim sStream As System.IO.FileStream
sStream = fiFile.Open(IO.FileMode.Open)


Once you are done with the file you can simply delete it.

CODE
fiFile.Delete()


If all you have to do is read or write to the contents then FileInfo may be over kill but if you want to copy, create, change the attributes, or delete the file then you will need FileInfo. Another great function is DirectoryInfo which does many of the same functions but to directories. While writing a program that does any type of file IO I have found that FileInfo.exists is indespensible in order to avoid run time errors involving file exception errors.

P.S. The only time I have found any descripiancy between VB.NET and C# was in version .NET 1.1. At the time I could not declare an unsigned 64-bit long in Visual Basic. Since then that has been cleared up and I know of absloutly nothing that can be done in C# and not Visual Basic.
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Apr 28 2008, 10:29 PM
Post #8


Premium Member
Group Icon

Group: [HOSTED]
Posts: 392
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322



QUOTE
Well that's not entirely accurate since. As you said, they both work on the same framework but they also both BUILD upon that same framework. Now both share probably 90+% of their functionality but there are certain features available in some .NET languages that others do not have. This is why they all exist In relation to file I/O this won't matter though heh.

When you are working with .net (or any language, but .net one's are quickest for this) it is VERY simple to google for example code blocks for basic functionality usage such as file I/O so in the future it might be more efficient for you to simply do that.


I was talking in the sense of the end result. Because all .NET languages compile to MSIL, what one can do, the other languages can accomplish too. Sure, some provide a bit easier ways to do them. Like the My object in VB. NET provides a lot of frequently used functions. C# code tends to be shorter than VB .NET because it is not bounded by the rule of being English like and uses symbols to quicken things up.

I am not sure they exist because of the differences in features. I believe they are there for different groups of programmers - those that have come up from the BASIC background and those from C/C++.

This post has been edited by turbopowerdmaxsteel: Apr 28 2008, 10:32 PM
Go to the top of the page
 
+Quote Post
tansqrx
post May 15 2008, 10:12 PM
Post #9


Super Member
Group Icon

Group: [HOSTED]
Posts: 533
Joined: 25-April 05
Member No.: 4,374



QUOTE(turbopowerdmaxsteel @ Apr 28 2008, 05:29 PM) *
I am not sure they exist because of the differences in features. I believe they are there for different groups of programmers - those that have come up from the BASIC background and those from C/C++.


Agreed
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. VB: Howto Auto-select Last Item In Listbox(5)
  2. VB6 - Previewing BB Code...(5)
  3. Delete A Registry Subkey And Key(8)


 



- Lo-Fi Version Time is now: 12th October 2008 - 02:29 AM