phreaky Posted December 18, 2002 Posted December 18, 2002 I already learn how to read from text files, but what I need to know now is how to write to them and the MSDN doesn't tell about it. Here it is what I'm doing Dim a As String Dim sw as IO.StreamWriter a = "whatever" IO.File.CreateText("file.txt") IO.File.OpenWrite("file.txt") sw.WriteLine(a) sw.Close() When I use this code it says that..... Additional information: The process cannot access the file "(path)\file.txt" because it is being used by another process. Quote tHe pHrEaKy
Moderators Robby Posted December 18, 2002 Moderators Posted December 18, 2002 here's one method... Private Function SetFileValue(ByVal path As String, ByVal fileName As String, ByVal value As String) As Boolean Try Dim sb As New System.IO.FileStream(path & "\" & fileName, System.IO.FileMode.OpenOrCreate) Dim sw As New System.IO.StreamWriter(sb) sw.Write(value) sw.Close() Return True Catch Return False End Try End Function Quote Visit...Bassic Software
phreaky Posted December 18, 2002 Author Posted December 18, 2002 Thanks dude, it worked! Quote tHe pHrEaKy
phreaky Posted December 26, 2002 Author Posted December 26, 2002 About writing and supplementing... Ok, now I know how to write a new file but, what if I want to substitute an existing file?, I mean, I have a .TXT file with a simple string, and I want to erase that string and put a new one, well, I mean, I need to rewrite completely a text file. Quote tHe pHrEaKy
*Gurus* divil Posted December 26, 2002 *Gurus* Posted December 26, 2002 I usually use this code to open a file and write to it, which overwrites the file if it's already there. Obviously, make sure you have the System.IO namespace imported. Dim f As StreamWriter f = New StreamWriter("file.txt", False) f.WriteLine("blah") f.Close() Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
phreaky Posted December 26, 2002 Author Posted December 26, 2002 Well, I already did try that before, but it doesn't overwrite it completely, because if you had a line that was much more larger than the new one you are trying to overwrite, then it will overwrite until it ends, but it keeps the rest, for example: if I have a string into the file that says: "my home is green and cute" and then I want to overwrite that string with "my name", then, the file finally records this "my name is green and cute" (that is using the 'write' command), by using the writeline command it will look like: "my name is green and cute" :( There should be anyway of when you open the file for writing it erases the old one completely and opens it (like a new one), like there's in some other languages. Quote tHe pHrEaKy
Dodgeram01 Posted December 26, 2002 Posted December 26, 2002 Is this what you want? Dim MyStream As New IO.FileStream("C:\blah.txt", IO.FileMode.Create) Dim MyWriter As New IO.StreamWriter(MyStream) MyWriter.Write("My Name" & vbCrLf & " is green and cute.") MyWriter.Close() MyStream.Close() Quote Dodge Grab Life By The Horns
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.