Meilleurs tutoriauxForumServicesPartenairesRégie publicitaireContactez-nous
S'inscrireConnexion

Accueil > Tutoriaux > Programmation > VB, VB.NET > Trucs & Astuces > Cinq astuces VB.net
Cinq astuces VB.net
Cinq astuces VB.net
antoine_bagnaudPar antoine_bagnaud, publié le 21/01/2012 à 20:37:30
Bonjour, aujourd’hui je vais vous donner cinq astuces en VB.net.

Astuce #1

On commence par une fonction permettant d'encrypter vos chaines de caractères en Sha1.

Function getSHA1Hash(ByVal strToHash As String) As String
Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider
Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)

bytesToHash = sha1Obj.ComputeHash(bytesToHash)

Dim strResult As String = ""

For Each b As Byte In bytesToHash
strResult += b.ToString("x2")
Next

Return strResult
End Function

Voilà donc pour un petit exemple :: http://www.multiupload.com/FOCEJKBRM4

Astuce #2
La seconde astuce n'étant pas facile à expliquer, c'est pourquoi j’ai fait une petite vidéo :: http://www.youtube.com/watch?v=3PJzTwhwnYk
Voilà donc le code qui est extrêmement simple.

Dim file As String = Command$()
If Not file = "" Then
file = Replace(file, Chr(34), "")
WebBrowser1.Navigate(file)
End If


Mettez-le de préférence au chargement du formulaire.

Voilà donc pour un petit exemple :: http://www.multiupload.com/ZZ4XGZCYMK

Astuce #3

Dans cette troisième astuce, nous allons voir comment parser un fichier texte pour récupérer des informations.

   Public Function GetFileDnn(ByVal file As String, ByVal Identifier As String) As String
Dim S As New IO.StreamReader(file) : Dim Result As String = ""
Do While (S.Peek <> -1)
Dim Line As String = S.ReadLine
If Line.ToLower.StartsWith(Identifier.ToLower & ":") Then
Result = Line.Substring(Identifier.Length + 2)
End If
Loop
Return Result
End Function


Le fichier doit avoir la structure suivante pour fonctionner.

propriété: argument
propriété: argument

Au niveau de la fonction, voici les arguments à mettre :

GetFileDnn('Chemain absolut vers le fichier', 'propriété') et la fonction retourne l'argument.

Voilà donc pour un petit exemple :: http://www.multiupload.com/8E6P0MWX38

Astuce #4

Dans cette quatrième astuce, nous allons voir comment vérifier si la machine dispose d'un accès internet.

Public Function IsConnectionAvailable() As Boolean
Dim objUrl As New System.Uri("http://www.google.com/")
Dim objWebReq As System.Net.WebRequest
objWebReq = System.Net.WebRequest.Create(objUrl)
Dim objResp As System.Net.WebResponse
Try
objResp = objWebReq.GetResponse
objResp.Close()
objWebReq = Nothing
Return True
Catch ex As Exception
objWebReq = Nothing
Return False
End Try
End Function


La fonction retourne un boleen, exemple :: If IsConnectionAvailable() = True Then

Voilà donc pour un petit exemple :: http://www.multiupload.com/33UD0H0VHP

Astuce #5

Dans cette dernière astuce, on va voir comment récupère un timestamp courant ou à partir d’une date définie.

   Public Function timestamp(ByVal date As Date) As Long
Dim origin As New Date(1970, 1, 1)
Dim span As TimeSpan = datefr - origin
Dim seconds As Double = span.TotalSeconds
Return CType(seconds, Long)
End Function


Et pour récupérer le timstamp courant, on met ceci :

timestamp(Date.Now)


Ajouter un commentaire
Vous voulez ajouter un message ? Créez un compte gratuitement !
Choisissez votre nom utilisateur :

(Déja membre ? Connexion)
Informations sur le tutoriel