Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.
tHe pHrEaKy
  • Moderators
Posted

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

Visit...Bassic Software
Posted

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.

tHe pHrEaKy
  • *Gurus*
Posted

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()

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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.

tHe pHrEaKy
Posted

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()

Dodge

Grab Life By The Horns

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...