martes, 30 de julio de 2019

servicio timer elapsed


SystemTimers.Timer

Timer1_Elapsed(Me, Nothing)

jueves, 25 de julio de 2019

ordenar elementos combobox

ComboBox1.Sorted = True


Buscar todos los archivos en todos los directorios

Dim array() As String = Directory.GetFiles(ruta_carpeta, "*", SearchOption.AllDirectories)

buscar texto en string con regex

Dim rgx As String = ".PDF"

Dim rx As Regex = New(rgx)

If rx.IsMatch("c:\archivo.PDF") Then

End If

carpetas directorios

Public Function get_fecha(ByVal f As String) As String
        Dim t As FileInfo = New FileInfo(f)
        Return t.Directory.Parent.Parent.ToString & t.Directory.Parent.ToString & t.Directory.Name
    End Function

lunes, 22 de julio de 2019

tabla en html

tr: table row (fila de tabla)
td: table data (celda de una tabla)


<table border="5" bordercolor="#ff0000">
<tr>
<td>Cell A</td>
<td>Cell B</td>
<td>Cell C</td>
</tr>
</table>

jueves, 18 de julio de 2019

lista de procesos

Imports System.IO



Public Class Form1

    Public t As Timer

    Private mtimers() As Timer


    Private sc1 As New Object
    Private sc2 As New Object

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim max As Integer = 2

        ReDim mtimers(max)
        For i As Integer = 0 To max
            t = New Timer

            If i = 0 Then
                t.Interval = 100 * 1
            ElseIf i = 1 Then
                t.Interval = 1000 * 1
            ElseIf i = 2 Then
                t.Interval = 1000 * 30
            End If

            t.Enabled = True
            t.Tag = i + 1
            AddHandler t.Tick, AddressOf Timer_Tick
            mtimers(i) = t
        Next

    End Sub

    Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        Dim timer As Timer = DirectCast(sender, Timer)
        Select Case timer.Tag
            Case "1"

                Dim th1 As Threading.Thread
                'th1 = New Threading.Thread(AddressOf function1)
                th1 = New Threading.Thread(Sub() Me.function1(7))
                th1.Start()

                'Dim sw As New StreamWriter("test1.txt", True)
                'sw.WriteLine(Now.ToString("HH:mm:ss"))
                'sw.Close()

                'While True
                'End While

                'timer.Stop()
            Case "2"
                'MessageBox.Show("TAG 2")
                Dim th2 As Threading.Thread
                th2 = New Threading.Thread(AddressOf function2)
                th2.Start()


                'Dim sw As New StreamWriter("test2.txt", True)
                'sw.WriteLine(Now.ToString("HH:mm:ss"))
                'sw.Close()


            Case 3
                Dim th3 As Threading.Thread
                th3 = New Threading.Thread(AddressOf function3)
                th3.Start()


        End Select
    End Sub


    Private Sub function1(ByVal id As Integer)

        SyncLock sc1

            'If id = 1 Then
            '    Exit Sub
            'End If

            'While True
            '    Threading.Thread.Sleep(1000)
            'End While


            Dim sw As New StreamWriter("test1.txt", True)
            sw.WriteLine(Now.ToString("HH:mm:ss"))
            sw.Close()

        End SyncLock
    End Sub

    Private Sub function2()
        SyncLock sc1
            Dim sw As New StreamWriter("test1.txt", True)
            'sw.WriteLine(Now.ToString("HH:mm:ss"))
            sw.WriteLine("hola!")
            sw.Close()

        End SyncLock
    End Sub


    Private Sub function3()
        Dim sw As New StreamWriter("test3.txt", True)
        sw.WriteLine(Now.ToString("HH:mm:ss"))
        sw.Close()
    End Sub


    Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click
        For Each t As Timer In mtimers
            t.Stop()
        Next
    End Sub

End Class
' https://www.dreamincode.net/forums/topic/140657-how-to-use-the-timer-as-array/
' http://vbcity.com/forums/t/36812.aspx
' https://social.msdn.microsoft.com/Forums/vstudio/en-US/ce7214c3-2116-4cfa-bb70-58ba2997cea7/stop-all-timers-at-once?forum=vbgeneral

threads con parámetros

 Dim th1 As Threading.Thread
th1 = New Threading.Thread(Sub() Me.function(1))
th1.Start()



Private Sub function1(ByVal id As Integer)
End Sub

fuente

lista de funciones con timer

Imports System.IO

Public Class Form1

    Public t As Timer

    Private mtimers() As Timer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReDim mtimers(1)
        For i As Integer = 0 To 1
            t = New Timer
            t.Interval = 1000 * (i + 1)
            t.Enabled = True
            t.Tag = i + 1
            AddHandler t.Tick, AddressOf Timer_Tick
            mtimers(i) = t
        Next

    End Sub

    Private Sub Timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
        Dim timer As Timer = DirectCast(sender, Timer)
        Select Case timer.Tag
            Case "1"
                Dim sw As New StreamWriter("test1.txt", True)
                sw.WriteLine(Now.ToString("HH:mm:ss"))
                sw.Close()
                'timer.Stop()
            Case "2"
                'MessageBox.Show("TAG 2")
                Dim sw As New StreamWriter("test2.txt", True)
                sw.WriteLine(Now.ToString("HH:mm:ss"))
                sw.Close()
        End Select
    End Sub

    Private Sub btn_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cancel.Click
        For Each t As Timer In mtimers
            t.Stop()
        Next
    End Sub

End Class

fuente1
fuente2
fuente3



log

Public Sub log(ByVal msj As String)
        Dim lista As New List(Of String)
        Try
            lista = File.ReadAllLines(archivo).ToList
            lista.Insert(0, msj)

            'Dim formatter As IFormatter
            'Using st As Stream = New MemoryStream
            '    formatter = New BinaryFormatter
            '    formatter.Serialize(st, lista)
            '    MessageBox.Show(st.Length \ 1024)
            'End Using

            File.WriteAllText(archivo, String.Empty)
            File.WriteAllLines(archivo, lista.ToArray, System.Text.Encoding.UTF8)
            lista.Clear()
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

miércoles, 17 de julio de 2019

crear log aplicación / servicio tamaño archivo

Public Sub log(ByVal mensaje As String)


        Dim filename As String = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) & "\" & "log.txt"

        If File.Exists(filename) Then
            Dim archivo_size As Long = New FileInfo(filename).Length / 1024
            If archivo_size >= 500 Then
                Try
                    Kill(filename)
                Catch ex As Exception
                End Try
            End If
        End If

        Dim tempfile As String = Path.GetTempFileName()
        If Not File.Exists(filename) Then
            File.Create(filename).Close()
        End If
        Using writer = New StreamWriter(tempfile)
            Using reader = New StreamReader(filename)
                writer.WriteLine("[" & Now.ToString("dd-MM-yyyy_HH:mm:ss") & "]-->" & mensaje)
                While Not reader.EndOfStream
                    writer.WriteLine(reader.ReadLine())
                End While
            End Using
        End Using
        File.Copy(tempfile, filename, True)
        File.Delete(tempfile)
    End Sub

fuente1

jueves, 11 de julio de 2019

Byte

Byte
[Byte]

transformar de byte a string ItemArray DataRow

Dim str As String = Encoding.Default.GetString(dr.ItemArray(5), 0, dr.ItemArray(5).Length)

Agregar columna a un ItemArray Datarow

ds.Tables(0).Columns.Add("CAF", Type.GetType("System.Byte[]"))
            If ds.Tables(0).Rows.Count > 0 Then
                For Each dr As DataRow In ds.Tables(0).Rows


tipos de dato para la columna

miércoles, 10 de julio de 2019

tcp socket windows service

fuente

número de elementos de una estructura

Public Structure empresa
        Dim rut As String
        Dim rsa As String
End Structure

Dim i As Integer = GetType(empresa).GetFields((BindingFlags.NonPublic Or (BindingFlags.Public Or BindingFlags.Instance))).Length

fuente

martes, 9 de julio de 2019

cambiar fondo textbox

TextBox1.BackColor = System.Drawing.SystemColors.Info

crear archivo xml

Public Sub crear_archivo_cfg(ByVal archivo As String)
        Dim sw As New StreamWriter(archivo, False, Encoding.UTF8)
        sw.WriteLine("<?xml version=""1.0"" encoding=""UTF-8""?>")
        sw.WriteLine("<DATA>")
        sw.Write("</DATA>")
        sw.Close()
    End Sub

eliminar nodo archivo xml

Public Sub eliminar_empresa(ByVal archivo As String, ByVal rut As String)
        Dim doc As New XmlDocument
        doc.Load(archivo)
        Dim nodos As XmlNodeList = doc.GetElementsByTagName("empresa")
        For Each nodo As XmlNode In nodos
            Dim x = nodo("rut").InnerXml
            If nodo("rut").InnerXml = rut Then
                nodo.ParentNode.RemoveChild(nodo)
                Exit For
            End If
        Next
        doc.Save(archivo)
    End Sub

viernes, 5 de julio de 2019

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