|
-
Oct 8th, 2005, 02:38 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] could it be better
i have changed an old vb6 application to 2005.net and i want to know if this sub (which does work) could be better, by that i mean have i used the right functions ?, are there better(faster) ones ?
this removes a username and every line under it until it reaches another username.
VB Code:
Public Sub RemoveUser(ByVal StringUser As String)
Dim sr As New IO.StreamReader("C:\usernamesinfo.txt")
Dim sw As New IO.StreamWriter("C:\usernamesinfo2.txt", True)
Dim Found As Boolean, Lines As String
Do While sr.Peek >= 0
Lines = sr.ReadLine
If Found = False Then
If InStr(Lines, "USERNAME" & StringUser) = 0 Then
sw.WriteLine(Lines)
Else
Found = True
End If
Else
If InStr(Lines, "USERNAME") > 0 Then
Found = False
sw.WriteLine(Lines)
End If
End If
Loop
sr.Close()
sw.Close()
IO.File.Delete("C:\usernamesinfo.txt")
IO.File.Move("C:\usernamesinfo2.txt", "C:\usernamesinfo.txt")
End Sub
thanks.
-
Oct 8th, 2005, 03:32 PM
#2
Frenzied Member
Re: could it be better
VB Code:
Public Sub RemoveUser(ByVal StringUser As String)
Dim sr As New IO.StreamReader("C:\usernamesinfo.txt")
Dim sw As StreamWriter
Dim al As New ArrayList
Dim i As Integer
Dim str As String
Dim arrTemp() As String
Dim Basket As New ArrayList
str = sr.ReadToEnd
sr.DiscardBufferedData()
sr.Close()
IO.File.Delete("C:\usernamesinfo.txt")
sw = IO.File.CreateText("C:\usernamesinfo.txt")
arrTemp = str.Split("USERNAME")
For i = 0 To arrTemp.Length - 1
If arrTemp(i).ToString.StartsWith(StringUser) Then
'Do nothing, this effectively removes it
Else
Basket.Add(arrTemp(i).ToString)
End If
Next
For i = 0 To Basket.Count - 1
sw.WriteLine("USERNAME" & Basket(i).ToString)
Next
sw.Flush()
sw.Close()
End Sub
Haven't tried it out ....but here it goes..
If you find my thread helpful, please remember to rate me 
-
Oct 8th, 2005, 03:57 PM
#3
Thread Starter
Hyperactive Member
Re: could it be better
thanks for your reply. its not exactly what i wanted but that s my fault for not showing a typical example of the file but on the other hand you have shown me a few more functions that i didnt know existed.
thank you very much.
-
Oct 8th, 2005, 05:50 PM
#4
Frenzied Member
Re: could it be better
no problems
could you explain what you are trying to do in greater detail?
If you find my thread helpful, please remember to rate me 
-
Oct 8th, 2005, 09:56 PM
#5
Re: could it be better
dinosaur_uk, note that the String.Split method cannot split on a substring, but only a single character. If you pass a string as the argument with Option Strict turned Off it will be implicitly converted to a character by taking just the first character. If Option Strict is turned On the code will not compile. There is an overload that takes an array of characters, but that splits on every instance of any one of those characters. You would need to use either the Split Runtime function or the Regex.Split method.
I would recommend against using a second file. Unless the file is really large, I would reas it like this:
VB Code:
Dim lines As String() = System.Text.RegularExpressions.Regex(myStreamReader.ReadToEnd(), Environment.NewLine)
If you really must use a second file, you should use the System.IO.Path.GetTempFile method to create a genuine temp file.
Once you have the lines of the file in the array, you can just loop over those strings and either edit them directly, or write them back to the same file name. Finally, use the String.IndexOf method instead of InStr. InStr is a Runtime function that would implicitly call String.IndexOf anyway, so why add the extar layer?
-
Oct 9th, 2005, 05:06 AM
#6
Frenzied Member
Re: could it be better
Ok ...cheers...didnt know that...
If you find my thread helpful, please remember to rate me 
-
Oct 9th, 2005, 09:21 AM
#7
Thread Starter
Hyperactive Member
Re: could it be better
with the suggestions of some functions and methods i have changed it all to this.
VB Code:
Public Sub RemoveUser(ByVal StringUser As String)
Dim sa As New ArrayList(IO.File.ReadAllLines("C:\usernamesinfo.txt"))
Dim start As Int16 = sa.IndexOf("USERNAME" & StringUser)
Dim i As Int16, counter As Int16
For i = start + 1 To sa.Count - 1
If sa(i).ToString.StartsWith("USERNAME") Then
Exit For
Else
counter += 1
End If
Next
sa.RemoveRange(start, counter + 1)
IO.File.WriteAllLines("C:\usernamesinfo.txt", sa.ToArray(GetType(String)))
End Sub
thanks for the direction.
-
Oct 9th, 2005, 10:41 AM
#8
Frenzied Member
Re: [RESOLVED] could it be better
If you find my thread helpful, please remember to rate me 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|