quinta-feira, 4 de fevereiro de 2016

Função para Gerar Código Hash em VB.Net

Imports System.Security.Cryptography
Imports System.Text
Public Function GeraHash(ByVal sTexto As String) As String
     Dim sSenha As String
     Dim SHA1hashValue() As Byte
     Dim b As Byte
     Dim UE As New UnicodeEncoding
     Dim MessageBytes As Byte() = UE.GetBytes(sTexto)
     Dim SHHash As New SHA1Managed

     SHA1hashValue = SHHash.ComputeHash(MessageBytes)
 
    sSenha = ""

    For Each b In SHA1hashValue
        sSenha &= b
 
   Next
    Return sSenha
End Function

Funções para manipular Banco de dados em VB.Net

Imports System.Data
Imports System.Data.SqlClient
Public Function LeBanco(ByVal strConexao As String, ByVal sSql As String) As DataSet
     Dim dataAdapter As SqlDataAdapter
     Dim DataSet As New DataSet
     Dim Conexao As SqlConnection
     Try
         Conexao = New SqlConnection(strConexao)
         Conexao.Open()
 
        dataAdapter = New SqlDataAdapter(sSql, Conexao)
         dataAdapter.Fill(DataSet, "Dados")

         Conexao.Close()
 
        Return DataSet

     Catch ex As Exception
         Return Nothing
     End Try
End Function



Public Function ExecutaBanco(ByVal strConexao As String, ByVal sSql As String, Optional ByVal InserirTransacao As Boolean = True) As Boolean
     Dim sAux As String
     Dim Conexao As SqlConnection = New SqlConnection(strConexao)
     Try
         'Define a transação
         If InserirTransacao = True Then
             sAux = "BEGIN TRAN " & sSql & " COMMIT TRAN"
         Else
             sAux = sSql
 
        End If
         Dim Comando As SqlCommand = New SqlCommand(sAux, Conexao)
         Conexao.Open()
         Comando.ExecuteNonQuery()
         Conexao.Close()
 
        Return True
     Catch ex As Exception
         Return False
     End Try
End Function

Alterar as cores dos controles dependendo da data em VB.Net

Public Sub AlteraCorDeFundo(ByVal Formulario As Control)
     Dim ctl As Control
     For Each ctl In Formulario.Controls
         Select Case System.DateTime.Now.Day
             Case 9 To 11
                 ctl.BackColor = Color.Black
                 ctl.ForeColor = Color.Yellow
                 Formulario.BackColor = Color.Black
             Case Else
                 ctl.BackColor = Color.LightGray
                 ctl.ForeColor = Color.Blue
                 Formulario.BackColor = Color.LightGray
         End Select
      Next
End Sub

Ler um arquivo texto com VB.Net

Imports System.IO
 
Public Function LerArquivoLocal(ByVal Arquivo As String) As String
    Dim fluxoTexto As IO.StreamReader
    Dim linhaTexto As String
    Dim Conteudo As String = String.Empty
        If IO.File.Exists(Arquivo) Then
            fluxoTexto = New IO.StreamReader(Arquivo)
            linhaTexto = fluxoTexto.ReadLine
            While linhaTexto <> Nothing
                Conteudo &= linhaTexto & vbCrLf
                linhaTexto = fluxoTexto.ReadLine
            End While

            fluxoTexto.Close()
            Return Conteudo
        Else
            Return "Erro."
        End If
End Function

Deletar arquivo em pasta FTP com VB.Net

Imports System.Net.Mail
Imports System.Net
Imports System.IO
Public Sub DeletarFtp(ByVal Arquivo As String, ByVal Usuario As String, ByVal Senha As String)
     Dim filename As String = "ftp://xxx.xxx.xxx.xxx/" & Arquivo
     Dim ftpReq As FtpWebRequest = WebRequest.Create(filename)
     ftpReq.Method = WebRequestMethods.Ftp.DeleteFile
     ftpReq.Credentials = New NetworkCredential(Usuario, Senha)
     Dim ftpResp As FtpWebResponse = ftpReq.GetResponse
End Sub