Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

The first code writes the text to the end line, but if used more than once, it puts all of the added text on the same line. How do I make it write to its own line every time?

 

Also, how can the second code be used to delete a line of text. I want to have to type the exact line that you want deleted for it to work.

 

VB Code: Add to File

 

Dim file As String

Dim StreamWriter As New IO.StreamWriter(TextBox2.Text, True)

StreamWriter.Write(TextBox3.Text)

StreamWriter.Close()

 

 

VB Code: Delete from File

 

Dim file As String

Dim StreamWriter As New IO.StreamWriter(TextBox2.Text)

StreamWriter.Write(TextBox4.Text)

StreamWriter.Close()

It's not impossible, it's Inevitable.
  • Leaders
Posted

With Sequential File Access streams, you don't actually "delete" a line from a file.

You just "conveniently forget to put it back in the file".

 

You'd want to have a Stream for Reading and then one for Writing.

You can use a StringBuilder also. (something like this)

Caption = SR.Readline()
[color=Teal]'Read a line from file.[/color]
[color=Blue]If[/color] Caption <> "asdfghjkl" [color=Blue]Then[/color]
 SB.Append(Caption)
 SB.Append(System.Environment.NewLine)
[color=Teal]'Conveniently forget to write the line "asdfghjkl" back to file.[/color]
[color=Blue]End If
Loop[/color]
SW.Write(SB.ToString())
[color=Teal]'Write the built string to file.[/color]

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted (edited)

I've got most of what you've said here, but SB? I assume that is Stringbuilder as you mentioned earlier. However, StringBuilder is not showing up as a Variable or whatever. Anything in Bold has an error.

 

VB Code:

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim file As String

Dim SR As New IO.StreamReader(TextBox2.Text)

Dim SW As New IO.StreamWriter(TextBox2.Text)

Dim SB As StringBuilder

 

file = (TextBox2.Text)

file = SR.ReadLine()

 

If file <> (TextBox4.Text) Then

SB.Append(file)

SB.Append(System.Environment.NewLine)

 

End If

Loop

SW.Write(SB.ToString())

 

End Sub

Edited by PureSc0pe
It's not impossible, it's Inevitable.
  • Leaders
Posted
[color=Blue]Dim[/color] SB [color=Blue]As[/color] System.Text.StringBuilder = [color=Blue]New[/color] System.Text.StringBuilder()

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

The Code shows no errors, but when I run the application it says:

An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll. Additional information: The process cannot access the file "C:\list.txt" because it is being used by another process.

It highlights Dim SW As New IO.StreamWriter(TextBox2.text) as the problem. Is it saying that because it's being used by StreamReader? If so, how would I fix that?

 

 

VB Code:

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim file As String

Dim SR As New IO.StreamReader(TextBox2.Text)

Dim SW As New IO.StreamWriter(TextBox2.Text)

Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder

 

file = (TextBox2.Text)

file = SR.ReadLine()

 

If file <> (TextBox4.Text) Then

SB.Append(file)

SB.Append(System.Environment.NewLine)

 

End If

SW.Write(SB.ToString())

 

End Sub

It's not impossible, it's Inevitable.
  • Leaders
Posted

Oh, yeah, you need to close the streamreader before you can open the streamwriter.

So, your code will look something like this.

Dim SR As IO.StreamReader = New StreamReader(...)

Dim SW As IO.StreamWriter

 

SR.Close()

SW = New StreamWriter(...)

SW.Write(...)

SW.Close()

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

Posted

Ok, when I run it, it deletes everything from the file except the first line. I need it to keep everything except what is typed into the texbox.

 

VB Code:

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Dim file As String

Dim SR As New IO.StreamReader(TextBox2.Text)

 

file = (TextBox2.Text)

file = SR.ReadLine()

 

SR.Close()

 

Dim SW As New IO.StreamWriter(TextBox2.Text)

Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder

 

If file <> (TextBox4.Text) Then

SB.Append(file)

SB.Append(System.Environment.NewLine)

End If

 

SW.Write(SB.ToString())

 

SW.Close()

End Sub

It's not impossible, it's Inevitable.
Posted
:D PureScope is going to be the Master of File I/O! :D

 

 

:D Hope so, but for now I still need help on what I said in my last post.

It's not impossible, it's Inevitable.
  • Administrators
Posted

You are opening your streamreader (SR) but then closing it after only 1 Readline. you would really need to do this in a loop. Try something like the following (not tested though so no promises)

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

	Dim line As String = ""

	Dim SR As New IO.StreamReader(TextBox2.Text)

	Dim SB As System.Text.StringBuilder = New System.Text.StringBuilder
	Do
		line = SR.ReadLine
		If line <> TextBox4.Text Then
			SB.Append(line)
			SB.Append(System.Environment.NewLine)
		End If
	Loop Until line = ""
	SR.Close()

	Dim SW As New IO.StreamWriter(TextBox2.Text, False)

	SW.Write(SB.ToString())
	SW.Close()

End Sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

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...