[HELP] My random code in text file need a little adjustment!
Hello! glad to talk with you again! you are very effective! !
ok i have a little problem with a code!
indeed, my code generate a random file but sometimes i have no file,error or a blank in a mediaplayer or other stuff!
here is my code:
Code:
Dim Randomtaunt As New Random()
Dim Tra As IO.TextReader = System.IO.File.OpenText(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt")
Dim FileLinest() As String = Split(Tra.ReadToEnd(), vbCrLf)
Tra.Close()
Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, UBound(FileLinest)))
Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
Me.AxWindowsMediaPlayer5.Ctlcontrols.play()
i suspect when the computer read my text file it could read a blank line. how must we fix it?
thank you in advance for your answer!
Re: [HELP] My random code in text file need a little adjustment!
Well, the first thing you need to do is break things into steps as it looks like you just randomly chucked what you found on google into a method.
Step one: Read the random class. Since the default random is time dependent you will generate the same seeded numbers.
https://docs.microsoft.com/en-us/dot...ew=netcore-3.1
step 2: read a text file using file.readalltext https://docs.microsoft.com/en-us/dot...System_String_
https://docs.microsoft.com/en-us/dot...ew=netcore-3.1
https://docs.microsoft.com/en-us/dot...veEmptyEntries
write the first parts then come back
Re: [HELP] My random code in text file need a little adjustment!
First, declare your random object at form level...
Code:
Dim Randomtaunt As New Random()
Then, be sure MyTAUNT <> ""...
Code:
Dim FileLinest() As String = io.file.readalllines(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt").where(function(f) f.Trim <> "").ToArray
Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, FileLinest.Length))
Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
Me.AxWindowsMediaPlayer5.Ctlcontrols.play()
Re: [HELP] My random code in text file need a little adjustment!
ok i will do my best! thank you for hins.
Re: [HELP] My random code in text file need a little adjustment!
My code checks the line isn't empty
Re: [HELP] My random code in text file need a little adjustment!
Quote:
Originally Posted by
.paul.
My code checks the line isn't empty
BIG THANK YOU PAUL! YOU helped me so much! you are a real gentleman! you code is perfect!
my regards
Re: [HELP] My random code in text file need a little adjustment!
If your issue is resolved then please use the Thread Tools menu to mark the thread Resolved. That will save anyone else opening the thread and reading six posts, only to find that there's no more help required.
Re: [HELP] My random code in text file need a little adjustment!
Quote:
Originally Posted by
danzey
BIG THANK YOU PAUL! YOU helped me so much! you are a real gentleman! you code is perfect!
my regards
Its not perfect and I guess you didnt read a single thing I posted and just accepted code instead of wanting to learn.
IsNullOrWhiteSpace
Trim the string is iterated through from each end.
Re: [HELP] My random code in text file need a little adjustment!
Quote:
Originally Posted by
ident
Its not perfect and I guess you didnt read a single thing I posted and just accepted code instead of wanting to learn.
IsNullOrWhiteSpace
Trim the string is iterated through from each end.
It reads the text file omitting any empty lines. That's all the OP wanted
Re: [HELP] My random code in text file need a little adjustment!
Quote:
Originally Posted by
.paul.
First, declare your random object at form level...
Code:
Dim Randomtaunt As New Random()
Then, be sure MyTAUNT <> ""...
Code:
Dim FileLinest() As String = io.file.readalllines(Application.StartupPath & "\movies\TAUNTVICTORY\" & Me.Label23.Text & ".txt").where(function(f) f.Trim <> "").ToArray
Dim MyTAUNT As String = FileLinest(Randomtaunt.Next(0, FileLinest.Length))
Me.AxWindowsMediaPlayer5.URL = Application.StartupPath & "\movies\TAUNTVICTORY\" & MyTAUNT
Me.AxWindowsMediaPlayer5.settings.setMode("loop", False)
Me.AxWindowsMediaPlayer5.Ctlcontrols.play()
That code should call File.ReadLines rather than File.ReadAllLines. The latter will read the whole file into an array first, then process that array and create another array, while the former will process each line as it's read from the file and only create one array when it's done. Also, while I realise it was copied from the OP, the path building should be done using Path.Combine. It's the same folder path but different file paths so the folder path should be built once and then that used to build each file path:
vb.net Code:
Dim folderPath = Path.Combine(Application.StartupPath, "movies\TAUNTVICTORY")
Dim filePath = Path.Combine(folderPath, Label23.Text & ".txt")