mshtml HTMLDocument createDocumentFromUrl in vb.net - vb.net

How to use createDocumentFromUrl in vb.net. I got information like we should use IPersistStreamInit interface if we want to work on this. So i want an example showing this.

I would not recomend going alone into that interface, But I am guessing that you are doing this in a dll/cmd line so you can't use the winform solution so you can use htmlAgility pack and you can download it as a nuget HtmlAgilityPack
But If you have to go the interop way you can check out this question Getting the HTML source from a WPF-WebBrowser-Control using IPersistStreamInit
And the only thing you need to add for vb is this
Public Overloads Sub Dispose() Implements IDisposable.Dispose
Dispose()
GC.SuppressFinalize(Me)
End Sub
But using the agility pack you can do it like this:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Net
Imports System.IO
Imports HtmlAgilityPack
Class Program
Private Shared Sub Main(args As String())
Dim wc As New WebClient()
Dim doc As New HtmlDocument()
doc.Load(wc.OpenRead("http://google.com"))
End Sub
End Class

Related

How would I fix this issue; Imports System.Security.Cryptography will not function with Imports System.Net

I am making a program in VB.Net which requires the imports of four import thingamajiggys. However, Imports System.Security.Cryptography will not work alongside each other imports.
I've tried arranging the order they work in. Imports System.Security.Cryptography will not work at all with Systems.Net, but it will work alongside Imports System.Text and Imports System.IO
Imports System.Security.Cryptography
Imports System.IO
Imports System.Net
Imports System.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Function SHA1(ByVal Content As String) As String
Dim Molecule As New Security.Cryptography.SHA1CryptoServiceProvider
Dim bytestring() As Byte = System.Text.Encoding.ASCII.GetBytes(Content)
bytestring = Molecule.ComputeHash(bytestring)
Dim finalstring As String = Nothing
For Each bt As Byte In bytestring
finalstring &= bt.ToString("x2")
Next
Return finalstring
End Function
A red wiggled line (like the spelling mistake in Word) appears under Security.Cryptography.SHA1CryptoServiceProvider but only when Systems.Net has been imported. Why does this happen?
You are getting the red wiggly line because System.Net also has a 'Security' namespace and it's trying to find Cryptography.SHA1CryptoServiceProvider in there.
To fix it either type
Dim Molecule As New System.Security.Cryptography.SHA1CryptoServiceProvider
or
Dim Molecule As New SHA1CryptoServiceProvider
You can use aliases.
Imports CryptoSecurity = System.Security
Imports MyNet = System.Net
Then in code
Dim Molecule As New CryptoSecurity.Cryptography.SHA1CryptoServiceProvider
You can use any names that make sense to you for the alias.

Defining implicit imports causes an error with other imports

I have an application built in VB.NET. Everything is working great, here are the imports I'm using....
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Data.Odbc
Imports System.Windows.Forms.Control
Imports System.Windows.Forms.DataGridView
Imports System.Runtime.InteropServices.Marshal
Imports System.IO
I'm trying to do something with excel and added the following import which causes me some issues.....
Imports Microsoft.Office.Interop.Excel
In Public Class I have this...
Private DtTable as DataTable
The errors are:
DataTable is ambiguous, imported from the namespaces or types
'Microsoft.Office.Interop.Excel, System.Data'.
also, I have a function where I use this as a paramter...
ByRef c As Windows.Forms.ComboBox
I get the following error...
Type Windows.Forms.Combobox is not defined.
You'll need to declare explicity your DataTable like this:
Private DtTable as Data.DataTable
The same for the second error. Declare it like this:
ByRef c As System.Windows.Forms.ComboBox
That is because the name DataTable is in multiple namespaces. Do it this way instead.
Imports Microsoft.Office.Interop
Private xlTable as Excel.DataTable
or, if you want the standard DataTable
Private dtTable as DataTable
And you missed the "System" part in the combox scope. But again, you are better doing it this way.
Imports System.Windows.Forms
ByRef c As ComboBox

Extracting Zip File in Visual Basic .NET

I am working on a Visual Basic Project and i am getting stuck on something super simple. Unzipping a file.
I have the following imports
`Imports System.Net
Imports System
Imports System.IO
Imports System.IO.Compression`
My References are as follows
System
System.Core
System.Data
System.Data.DataSetExtensions
System.Deployment
System.Drawing
System.IO.Compression
System.IO.Compression.FileSystem
System.Net.Http
System.Windows.Forms
System.Xml
System.Xml.Linq
So what my code should be doing is checking if a software is installed,
if it is not it will download a zip file with the installed.
once the zip is downloaded it should extract it and run the setup.
Everything is working except this code block right here:
Private Sub client_OMSADownloadCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
MessageBox.Show("Download Complete")
Try
ZipFile.ExtractToDirectory("C:\end.zip", "C:\end")
Catch ex As Exception
MsgBox("Can't Extract file" & vbCrLf & ex.Message)
End Try
End Sub
Public NotInheritable Class ZipFile
Public Shared Sub ExtractToDirectory(
sourceArchiveFileName As String,
destinationDirectoryName As String
)
End Sub
End Class
I get no exceptions, it just doesn't unzip, it basically skips right over the block.
Please help!
According your code, you have inherit the method ExtractToDirectory of ZipFile class that do nothing.
Public NotInheritable Class ZipFile
Public Shared Sub ExtractToDirectory(sourceArchiveFileName As String,
destinationDirectoryName As String)
End Sub
End Class
To solve this issue, simple delete this method from your code.
You're declaring ZipFile class yourself, while you should use existing one from System.IO.Compression namespace. So, just remove following part of your code:
Public NotInheritable Class ZipFile
Public Shared Sub ExtractToDirectory(
sourceArchiveFileName As String,
destinationDirectoryName As String
)
End Sub
End Class
...and everything should work as expected.

Strange 'Dispose' Error when calling with 'Using' or directly calling 'DIspose'

I have this code which works:
Option Strict On
Imports System
Imports System.IO
Imports System.ServiceModel
Imports System.ServiceModel.Description
...
Private Property Channel As IWSOServiceContract
Public Sub SomeMethod(ByVal url As String)
Using ChlFactory As ChannelFactory(Of IWSOServiceContract) = New ChannelFactory(Of IWSOServiceContract)(New WebHttpBinding(), url)
ChlFactory.Endpoint.Behaviors.Add(New WebHttpBehavior())
Channel = ChlFactory.CreateChannel()
End Using
End Sub
...
But then when I refactor it to:
Option Strict On
Imports System
Imports System.IO
Imports System.ServiceModel
Imports System.ServiceModel.Description
...
Private Property ChlFactory As ChannelFactory(Of IWSOServiceContract)
Private Property Channel As IWSOServiceContract
Public Sub SomeMethod(ByVal url As String)
ChlFactory = New ChannelFactory(Of IWSOServiceContract)(New WebHttpBinding(), url)
ChlFactory.Endpoint.Behaviors.Add(New WebHttpBehavior())
Channel = ChlFactory.CreateChannel()
ChlFactory.Dispose() '<-- ERROR HERE BUT HOW DID USING WORK BEFORE?
End Sub
...
Am completely at a loss without any explanation of why there is an error "Dispose is not a member of ChannelFactory" in the second method but not in the first method?
That's because ChannelFactory implements IDisposable.Dispose explicitly. You'd have to cast it to IDisposable to call Dispose.
Using statement is smart enough to do the casting for you.

Why the XUnit attributs aren't recognised?

I installed the libraries xUnit with NuGet, and added references but I have errors because of the attributes [Theory], [InlineData("11/12/2011","2011-11-12")] and [Fact].
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Linq
Imports System.Text.RegularExpressions
Imports FluentAssertions
Imports Xunit
Imports System
Imports System.Diagnostics
Imports Xunit.Extensions
Public Class XUnitClassTest
[Theory]
[InlineData("11/12/2011","2011-11-12")]
Public Sub test(input As String, output As String)
Dim pattern As String = "\d+|[A-Za-zÀàÂâÄäÇçÉéÈèÊêËëÎîÏïÔôÖöÙùÛûÜü']+"
Dim matchList As MatchCollection = Regex.Matches(input, pattern)
Dim matchArray(matchList.Count - 1) As Match
matchList.CopyTo(matchArray, 0)
Dim manager As Processeur = New Processeur
manager.GetData(matchArray.Select(Function(a) a.ToString())).Should().Be(output)
End Sub
[Fact]
Public Sub FactMethodName()
Write(DateTime.Parse("1658").ToString())
End Sub
Public Shared Sub Write(format As String, ParamArray param As Object())
Console.WriteLine(format, param)
End Sub
End Class
You are using C# attribute syntax in VB.NET.
VB.NET syntax would be
<Fact>
Public Sub FactMethodName()
Write(DateTime.Parse("1658").ToString())
End Sub
and so on.