I'd like to download a webpage using .Net's WebClient class, extract the title (i.e. what's between <title> and </title>) and save the page to a file.
The problem is, the page is encoded in UTF-8 and the System.IO.StreamWriter throws an exception when using a filename with such characters.
I've googled and tried several ways to convert UTF8 to ANSI, to no avail. Does someone have working code for this?
'Using WebClient asynchronous downloading
Private Sub AlertStringDownloaded(ByVal sender As Object,
ByVal e As DownloadStringCompletedEventArgs)
If e.Cancelled = False AndAlso e.Error Is Nothing Then
Dim Response As String = CStr(e.Result)
'Doesn't work
Dim resbytes() As Byte = Encoding.UTF8.GetBytes(Response)
Response = Encoding.Default.GetString(Encoding.Convert(Encoding.UTF8,
Encoding.Default, resbytes))
Dim title As Regex = New Regex("<title>(.+?) \(",
RegexOptions.Singleline)
Dim m As Match
m = title.Match(Response)
If m.Success Then
Dim MyTitle As String = m.Groups(1).Value
'Illegal characters in path.
Dim objWriter As New System.IO.StreamWriter("c:\" & MyTitle & ".txt")
objWriter.Write(Response)
objWriter.Close()
End If
End If
End Sub
Edit: Thanks everyone for the help. It turns out the error was not due to UTF8 but rather a hidden LF character in title section of the page, which is obviously an illegal character in a path.
Edit: Here's a simple way to remove some of the illegal characters in a filename/path:
Dim MyTitle As String = m.Groups(1).Value
Dim InvalidChars As String = New String(Path.GetInvalidFileNameChars()) + New String(Path.GetInvalidPathChars())
For Each c As Char In InvalidChars
MyTitle = MyTitle.Replace(c.ToString(), "")
Next
Edit: And here's how to tell WebClient to expect UTF-8:
Dim webClient As New WebClient
AddHandler webClient.DownloadStringCompleted, AddressOf AlertStringDownloaded
webClient.Encoding = Encoding.UTF8
webClient.DownloadStringAsync(New Uri("www.acme.com"))
I don't think the problem is related to UTF-8. I think your regex will include </title> if it appears on the same line. The characters<> are invalid in a Windows filename.
If this is not the problem it would be helpful to see some sample input and output values of MyTitle.
Related
I'm trying to open a file that I already add to my TreeView control by clicking it twice, it should appears in a DataGridView control, when a I do it, it shows me the next error:
System.InvalidCastException: 'Conversion from string "Book1.csv" to type 'Integer' is not valid.'
At the Direccion variable, I'm not pretty sure, what it happening. Does any one could orient me? Please.
Public Sub TV_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As
TreeNodeMouseClickEventArgs) Handles TV.NodeMouseDoubleClick
Dim NombreNodo As String = TV.SelectedNode.Text
Dim parseCSV As String
Dim tstSeq() As String
Dim Direccion As String = My.Computer.FileSystem.CurrentDirectory(NombreNodo)
'Dim x As String = Path.GetFullPath(NombreNodo)
'MessageBox.Show(Direccion)
tstSeqDataGrid.Rows.Clear()
Using FileSystem As FileStream = File.Open(Direccion, FileMode.Open, FileAccess.Read)
Dim TestReader As New System.IO.StreamReader(FileSystem)
Do While TestReader.Peek <> -1
parseCSV = TestReader.ReadLine()
tstSeq = parseCSV.Split(",")
tstSeqDataGrid.Rows.Add(tstSeq)
TstSequenceLoaded = True
Loop
TestReader.Close()
FileSystem.Close()
End Using
End Sub
I am assuming that the TreeView is loaded with file names and those files are located in the directory where the code is running. You can see the value of Direccion in the Immediate Window. Using Debug.Print instead of a message box saves you from the embarrassment of forgetting to remove the message box in the production code. The Debug.Print will just be removed.
I returned an array of lines in the file with ReadAllLines. Then loop through the lines as you did.
With Option Strict On (as it should be) you need to add the lower case c following "," so the compiler knows you intend it as a Char not a String.
Public Sub TV_NodeMouseDoubleClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles TV.NodeMouseDoubleClick
Dim Direccion = My.Computer.FileSystem.CurrentDirectory & TV.SelectedNode.Text
Debug.Print(Direccion)
tstSeqDataGrid.Rows.Clear()
Dim lines = File.ReadAllLines(Direccion)
For Each line In lines
Dim tstSeq = line.Split(","c)
tstSeqDataGrid.Rows.Add(tstSeq)
Next
TstSequenceLoaded = True
End Sub
I have this code to programmatically export a report to PDF.
Public Sub Main()
TRY
DIM historyID as string = Nothing
DIM deviceInfo as string = Nothing
DIM extension as string = Nothing
DIM encoding as string
DIM mimeType as string = "application/Excel"
DIM warnings() AS Warning = Nothing
DIM streamIDs() as string = Nothing
DIM results() as Byte
rs.Credentials = System.Net.CredentialCache.DefaultCredentials
Dim dataSources() As DataSource = rs.GetItemDataSources("foldername/reportname")
rs.LoadReport(REPORTSERVER_FOLDER, historyID)
results = rs.Render(FORMAT, deviceInfo, extension, mimeType, encoding, warnings, streamIDs)
DIM stream As FileStream = File.OpenWrite(FILENAME)
stream.Write(results, 0, results.Length)
stream.Close()
Catch e As IOException
Console.WriteLine(e.Message)
End Try
End Sub
When I run it I get an error saying:
error BC30002: Type 'DataSource' is not defined.
Dim dataSources() As DataSource = rs.GetItemDataSources("foldername/reportname")
~~~~~~~~~~
Am I forgetting to import something? If I remove that line it works fine (besides that it needs a data source to be added). Adding the data source beforehand is not an option.
So I figured the answer. I am using the Exec2005 (execution endpoint), which does not include the definition for DataSource. I should instead use the default endpoint (Mgmt2005), but this causes other problems in the code.
In any case, the answer to this question is to not use -e Exec2005.
I am trying to search a text document, and I am at a stand still.
Example of the document:
11/24 05:05:21.781 T0EA8 [PinRegister Version: PINREG 1.2.0]
11/24 05:05:21.875 T0EA8 [RequestPinPadParamEvent: PR_RegDevice = 0.Exit]
11/25 05:04:38.906 T0FB0 [*************************: ]
11/25 05:04:38.906 T0FB0 [PinRegister Version: PINREG 1.3.0]
Now, in that example document, I want to display the 'PinPegister Version' to textbox 'VersionTextBox' from the 25th.
So I am trying to search for the date, then from the date search for the phrase ('[PinRegister Version: ]') and finaly display the results into the textbox ('PINREG 1.3.0').
I have tried alot of options, with nothing working how I want it.
This is my current code, and I feel close... but I am getting an error 'Object reference not set to an instance of an object.'
Dim strm As IO.Stream = IO.File.OpenRead(fpath)
Dim sr As New IO.StreamReader(strm)
Dim line As String
Dim trimchars() As Char = {" "c}
Dim datelist As ArrayList
Do While sr.Peek <> -1
line = sr.ReadLine()
'If line.TrimStart(trimchars).Contains("[PinRegister Pin Pad Model") Then
If line.TrimStart(trimchars).StartsWith(TDate.Text) Then
' found pattern
datelist.Add(line)
End If
Loop
If datelist.Contains("PinRegister Version:") Then
MsgBox("Found 1")
End If
Thanks in advance for any help
Couldn't you just do something like this?
Public Shared Sub FindStuff()
Dim TextFileLocation = "C:\Test\Test.txt"
Dim srReader As IO.StreamReader = Nothing
srReader = File.OpenText(TextFileLocation)
Do
Dim strInputFileLine As String = srReader.ReadLine()
If strInputFileLine Is Nothing Then Exit Do
If strInputFileLine.Contains(Form1.TextBox1.Text) Then
MessageBox.Show("Found it")
End If
Loop
You didn't say how big your input file is, but if its not too large I would suggest reading in all into a string
My.Computer.FileSystem.ReadAllText
and then using a
yourstring.indexof( ...
to find the text you are looking for and work it from there.
I've done this in the past and its quite fast in most cases.
Well I get this error (The ID3v2TagVersions value of '4' (4) is not valid for this operation.) with UltraID3Lib on Visual Basic 2013. I want to use this library (dll) so i can edit my mp3 tags in a mp3 file. Although I achived changing the tags , when i use the Clear() sub and then retry to change the tags i get this error. Can anyone help me ?
My Code
Private Sub btnExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim Artist As String = ""
Dim Title As String = ""
Dim MP3TagEditor As New UltraID3
For Each Path In MP3List
MP3TagEditor.Read(Path)
Title = "Somthing"
Artist = "Somthing"
MP3TagEditor.ID3v2Tag.Title = Title
MP3TagEditor.ID3v2Tag.Artist = Artist
MP3TagEditor.Clear()
MP3TagEditor.Write()
Next
MsgBox("Tags Added", MsgBoxStyle.Information, "Success")
End Sub
Thanks
Try this
Dim sTitle As String = "No Name"
Dim sSinger As String = ""
Dim sAlbum As String = ""
Dim sYear As String = ""
Dim sComm As String = ""
Dim MP3Tag As New UltraID3
MP3Tag.Read(YOUR_PATH)
Try
Dim pics = MP3Tag.ID3v2Tag.Frames.GetFrames(CommonMultipleInstanceID3v2FrameTypes.Picture)
AlbumPic.Image = CType(pics(0), ID3v2PictureFrame).Picture
Catch ex As Exception
AlbumPic.Image = My.Resources.FlatCD
End Try
Try
sTitle = MP3Tag.Title
sSinger = MP3Tag.Artist
sAlbum = MP3Tag.Album
Catch ex As Exception
End Try
You cannot read ID3v2.4 tags from an MP3 file with UltraID3Lib. It doesn't support it yet. (As of 31/12/2015).
But there is an alternative, which, in my humble opinion is even better, stable and efficacious:
TagLib-Sharp
It supports many other tag types as well apart from ID3, i.e, MP3 Files.
A quick example to get you on the way:
Dim f As TagLib.File = TagLib.File.Create("someFile.mp3")
Dim artist As String = f.Tag.JoinedPerformers
Dim title As String = f.Tag.Title
More resources for TagLib-Sharp:
Where can I find tag lib sharp examples?
Set Bitmap as cover art for MP3
Hello people as the topic show I want to do in VB.net a script that gets only the numbers from a *.txt file
EXAMPLE:
text file:
asd4lkj5fdl
jklj235
the result:
45235
I have done a research in Google and come up with nothing, I did saw a answer here but only in C
I know that in theory it needs to be like this:
Read every char loop ask if it is an integer add it to a new string if not continue to next char do this till the end of the stream
Thanks for the one how helps !
Try regular expressions, code to read text from file is no included
Dim rgx As New Regex("[^\d]")
Dim result as String = rgx.Replace("asd4lkj5fdl jklj235", "")
Read File into a String
Loop through each char checking if numeric.
Dim strTextFromFile As String = IO.File.ReadAllText("C:\filename.txt")
Dim strResults As String = String.Empty
For Each c As Char In strTextFromFile
If IsNumeric(c) Then
strResults += c
End If
Next
MsgBox(strResults)
Public Sub Test()
Dim contents As String = File.ReadAllText("C:\\temp\\text.txt")
Dim digits As New String(contents.Where(Function(c) Char.IsDigit(c)).ToArray())
MessageBox.Show(digits)
End Sub