Axis
May 17 2005, 03:33 PM
| | 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? |
Reply
miCRoSCoPiC^eaRthLinG
May 17 2005, 04:48 PM
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 readerCODE 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
Reply
iGuest
Apr 11 2008, 01:35 AM
Replying to miCRoSCoPiC^eaRthLinGYou 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
Reply
iGuest
Apr 27 2008, 05:26 PM
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.
Reply
turbopowerdmaxsteel
Apr 28 2008, 03:29 PM
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.
Reply
Jeigh
Apr 28 2008, 08:18 PM
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.
Reply
tansqrx
Apr 28 2008, 10:26 PM
Hope you don’t mind but I am going to reply to the original post  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.
Reply
turbopowerdmaxsteel
Apr 28 2008, 10:29 PM
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++.
Reply
tansqrx
May 15 2008, 10:12 PM
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
Reply
Recent Queries:--
add remove delete function query in vb.net - 1.22 hr back. (1)
Similar Topics
Keywords : vb, net, howto, add, delete, files, code
- Delete A Registry Subkey And Key
Using Visual Basic 2005 Express (8)
VB6 - Previewing BB Code...
(5) I'm using Visual Basic 6 and want to know how (if possible) to create a program to preview BB
code. Like the codes here using InvisionBoard with the " " tag for bold as an example. I want my
program to allow the user to enter BB code and convert them to formatted text. Is this possible?
Thanks.....
How Do I Write Code Using WinSock Under .NET ?
(4) I used to use the WinSock control in the traditional VB6 to make connections between computers. But
now, I've moved to VB.NET and I want to know how can I do the same thing in .NET. Is there any
.NET class that have the same function as WinSock 2? Please help me, I may need some example on how
to use that class (code in VB.NET, please).....
VB: Howto Auto-select Last Item In Listbox
(5) Hi, I'm just writing a small program with a friend that is kind of pointless at the moment
/smile.gif' border='0' style='vertical-align:middle' alt='smile.gif' /> but there was one issue
that we haven't been able to work out with a listbox and we aren't too sure if its even
possible to do it. /unsure.gif' border='0' style='vertical-align:middle' alt='unsure.gif' /> My
question is, is it possible to make a listbox automatically scroll to the bottom of the list when a
new item is added? if so can anyone please let me know? Thank you all in advance for ....
VS.NET: Editing Designer Generated Code Problem
Old Problem - any workarounds ? (1) HI, This is a very old & well-known bug in Visual Studio 2003 and I hear it's been recitifed
in the 2005 version. But till I get my hands on it (which isn't hapenning soon enough) I still
got to use the old VS2003. The problem is: if you mess around with the code that lies in the region
"Windows Forms Designer Generated Code" - the form controls just VANISH when you load it up in
designer mode. They just won't come back however hard you try - and all you're left with is
a BLANK form. Not that it hampers with your code from running in any way - but you....
Looking for vb, net, howto, add, delete, files, code
|
|
Searching Video's for vb, net, howto, add, delete, files, code
|
advertisement
|
|