Jump to content
xisto Community
Sign in to follow this  
Axis

VB.NET: Howto Add And Delete Files Just looking for useful code

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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

 

Imports System.IO.......'Just a var to hold your filenameDim FilePath As String = "c:\somefile.txt"'Define a stream reader and link it to your fileDim sr As StreamReader = File.OpenText (FilePath)'Read the file to the end and load it in your rtf boxrtfBox.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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

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

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

fiFile.Create()

You can also copy it to another location.

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.

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

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

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.

Share this post


Link to post
Share on other sites

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++.
Edited by turbopowerdmaxsteel (see edit history)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

FileSystem.Kill(file_location) 'will remove the file For people who get here via google search and nothing else works (I am using 2008 pro :D took me ages to figure it out/guess) -reply by alex3410

Share this post


Link to post
Share on other sites
FileSystem.Kill vs. File.DeleteVB.NET: Howto Add And Delete Files

Way to go Alex3410!  That saved me a lot of time.  I had trouble getting a file to go away.  If you use File.Delete(fname) you must make sure another process is not using it.  Otherwise you can use FileSystem.Kill(fname) which will just go ahead and delete the file.

David

Share this post


Link to post
Share on other sites

I didn’t realize that there is a kill function for files. I see that this is a carryover from VB6 and is generally frowned upon in .NET (https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.kill.aspx). The MSDN article also states that the kill functions requires Read and PathDiscovery flags of FileIOPermissions.For better understandable code I would still use the System.IO.FileInfo methods discussed earlier.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.