jueves, 4 de julio de 2019

descargar archivo

Imports System.Net
Imports System.Text

Public Class Form1

    Private Sub btn_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_ok.Click

        Dim wc As New WebClient
        Dim result() As Byte = Nothing
        Dim str As String

        Try
            result = wc.DownloadData("")
            str = Encoding.UTF8.GetString(result)
            Dim pause As String = ""
        Catch ex As Exception
        End Try
    End Sub

    Private Sub btn_async_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_async.Click

        Dim url As String = ""
        Dim wc As New WebClient
        AddHandler wc.DownloadDataCompleted, AddressOf DownloadDataCompleted
        wc.DownloadDataAsync(New Uri(url))
    End Sub

    Private Shared Sub DownloadDataCompleted(ByVal sender As Object, ByVal e As DownloadDataCompletedEventArgs)

        Dim str As String
        Dim raw() As Byte = e.Result
        str = Encoding.UTF8.GetString(raw)
        Dim pause As String = ""

    End Sub


End Class
' https://stackoverflow.com/questions/2375896/vb-net-downloaddataasync
' https://stackoverflow.com/questions/1585985/how-to-use-the-webclient-downloaddataasync-method-in-this-context
' https://stackoverflow.com/questions/35056500/download-file-from-google-drive-using-c-sharp-without-google-api
' https://stackoverflow.com/questions/19686599/download-file-directly-to-memory

viernes, 28 de junio de 2019

warning windows service

The target version of the .NET Framework in the project does not match the .NET Framework launch condition version '.NET Framework 4'. Update the version of the .NET Framework launch condition to match the target version of the .NET Framework in the Advanced Compile Options Dialog Box (VB) or the Application Page (C#, F#).





1. Setup
2. Click mouse right button and select View options Launch Conditions
3. Select Launch conditions properties and change version framework prerequisites, example: .NET Framework 3.5

















fuente

miércoles, 26 de junio de 2019

Obtener el primer directorio después del directorio raíz

Dim dir As System.IO.FileInfo = New System.IO.FileInfo(nombre_archivo)
dir.Directory.Parent.FullName

martes, 25 de junio de 2019

obtener el nombre del directorio de un archivo

ejemplo:

c:\tareas\archivo.xml --->tareas

return Path.GetFileName(Path.GetDirectoryName(c:\tareas\archivo.xml))

fuente

The underlying connection was closed

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

solución:

ServicePointManager.ServerCertificateValidationCallback = AddressOf ValidateRemoteCertificate

 'Public Shared Function ValidateRemoteCertificate(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal sslPolicyErrors As Net.Security.SslPolicyErrors) As Boolean
    '    Return True
    'End Function


fuente

viernes, 21 de junio de 2019

archivo bloqueado


Dim ok As Boolean = file_is_lock(New FileInfo(archivo))


Public Function file_is_lock(ByVal f As FileInfo) As Boolean
        Dim stream As FileStream = Nothing
        Try
            stream = f.Open(FileMode.Open, FileAccess.Read, FileShare.None)
        Catch ex As Exception
            Return True
        Finally
            If Not stream Is Nothing Then
                stream.Close()
            End If
        End Try
        Return False
    End Function

lunes, 17 de junio de 2019

guardar xml con caracteres especiales < > / &


Public Sub guardar_query_cv(ByVal nodo As String, ByVal query_sql As String)
        Dim doc As New XmlDocument
        doc.Load(Directory.GetCurrentDirectory & "\" & Form1.archivoConfiguracion)
        Dim xnode As XmlNode = doc.GetElementsByTagName(nodo)(0)
        xnode.InnerXml = System.Security.SecurityElement.Escape(query_sql)
        doc.Save(Directory.GetCurrentDirectory & "\" & Form1.archivoConfiguracion)
    End Sub


fuente

fuente1

fuente2

jueves, 13 de junio de 2019

obtener el número de línea achivo xml

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xdoc As XDocument = XDocument.Load("archivo.xml", LoadOptions.SetLineInfo)
        Dim categories As IEnumerable(Of XElement) = xdoc.Descendants("TipoDespacho")
        For Each category As XElement In categories
            Dim lineNumber As Integer = CType(category, IXmlLineInfo).LinePosition
        Next
    End Sub

TabControl TabPage agregar/remover

TabControl1.TabPages.Remove(TabPage1)
TabControl1.TabPages.Remove(TabPage2)

fuente