Weblogs Código

RecorteX

» vbnet
73 usuarios y 243 recortes de código
Usuario

Contraseña
Crear cuenta
Estás viendo los recortes de la etiqueta vbnet

RSS
Recortes: 28

Etiquetas relacionadas:
csharp
xml
vb
2005
signo
double
validacion
negativos
sql
dropdown
net
asp
string
forma
ventana
numero
titulo
formato
version
pesos
dataset
url
items

Coleccion de datos en net

Esto se usa para hacer una coleccion de un mismo objeto

 
  Public Class ProcesosBase
    Inherits CollectionBase
 
    Public Sub New()
    End Sub
 
    Public ReadOnly Property Item(ByVal index As Integer) As CLASE_A_USAR
        Get
           Return CType(List.Item(index), CLASE_A_USAR)
        End Get
    End Property
 
    Public Function Add(ByVal value As CLASE_A_USAR) As Integer
        Dim res As Integer
        res = List.Add(value)
        Return res
    End Function
 
End Class
 

Traer un DT desde un query en SqlServer

 
    Private Function getDT(ByVal qry As String, ByVal connectionString as string) As DataTable
        Dim dataSet As DataSet = New DataSet
        Dim conn As New SqlConnection(connectionString)
        Dim cmdString As String = qry
        Dim dataAdapter As New SqlDataAdapter(cmdString, conn)
        Dim ds As New DataSet()
        dataAdapter.Fill(ds, "dt")
 
        Return ds.Tables("dt")
    End Function
 
 
    private DataTable getDT(String qry, String  connectionString)
    {
        DataSet dataSet = new DataSet();
        SqlConnection conn = new SqlConnection(connectionString);
        SqlDataAdapter dataAdapter = new SqlDataAdapter(qry, conn);
        DataSet ds= new DataSet();
        dataAdapter.Fill(ds, "dt");
  
        return ds.Tables ["dt"];
    }
 

XMLtoDT

 
    Private Function XMLtoDT(ByVal xml As String) As DataTable
        Dim dataSet As DataSet = New DataSet
        Dim reader As New System.Xml.XmlTextReader(New StringReader(xml))
        dataSet.ReadXml(reader)
        If dataSet.Tables.Count = 0 Then
            Return New DataTable
        Else
            Return dataSet.Tables(0)
        End If
    End Function
 

Funcion para checar si un valor esta dentro de una serie separada por comas

 
    Function IsIn(ByVal val1 As String, ByVal text As String) As Boolean
        Dim ret As Boolean = False
        Dim temp() As String
        Dim separador As String = ","
        temp = Microsoft.VisualBasic.Split(text, separador)
        For Each b As String In temp
            If val1 = b Then
                ret = True
                Exit For
            End If
        Next
        Return ret
    End Function
 

Salir de Console App regresando un ErrorLevel

Este ejemplo regresa ErrorLevel=1

 
         dim ErrorLevel as integer=1
         Environment.Exit(ErrorLevel)
 

Recursividad

Un ejemplo de recursividad para buscar una cadena en archivos.

 
      Public Sub CheckFiles(ByVal TxtToFind As String, ByVal Path As String, ByRef sb As System.Text.StringBuilder, ByVal Filter As String)
        For Each file As String In Directory.GetFiles(Path, Filter)
            Dim txt As String = ReadFile(file)
            If txt.IndexOf(TxtToFind) <> -1 Then
                sb.AppendLine(file)
            End If
        Next
        For Each dir As String In Directory.GetDirectories(Path)
            CheckFiles(TxtToFind, dir, sb, Filter)
        Next
    End Sub
 

Leer y escribir archivos de texto

En Vb.net

 
    Private Sub WriteFile(ByVal FileName As String, ByVal data As String)
        If File.Exists(FileName) Then File.Delete(FileName)
        Dim swFile As StreamWriter = File.CreateText(FileName)
        swFile.Write(data)
        swFile.Flush()
        swFile.Close()
    End Sub
 
    Private Function ReadFile(ByVal FileName As String) As String
        Dim ret As String = ""
        If File.Exists(FileName) Then
            Dim TheFile As System.IO.StreamReader = New StreamReader(FileName, System.Text.Encoding.Default)
            ret = TheFile.ReadToEnd()
            TheFile.Close()
        End If
        Return ret
    End Function
 

En c#

 
        public static void WriteFile(String FileName , String data ){
            if (File.Exists(FileName)) {File.Delete(FileName);}
            StreamWriter swFile   = File.CreateText(FileName);
            swFile.Write(data);
            swFile.Flush();
            swFile.Close();
        }
 
        public static String ReadFile(String FileName ) {
            String ret  = "";
            if (File.Exists(FileName)){
                 System.IO.StreamReader TheFile = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
                 ret = TheFile.ReadToEnd();
                 TheFile.Close();
            }
            return ret;
        }
 

Leer recursos de la aplicación

 
Imports System.IO
Imports System.Reflection
 
 
    Private Function ReadStringResource(ByVal resource As String) As String
        Dim asm As Assembly = Assembly.GetExecutingAssembly()
        Dim stream As Stream = asm.GetManifestResourceStream(resource)
        Dim streamReader As StreamReader = New StreamReader(stream)
        Dim ret As String = streamReader.ReadToEnd
        Return ret
    End Function
 
 
        private String ReadStringResource(String resource) {
            Assembly asm = Assembly.GetExecutingAssembly();
            Stream stream = asm.GetManifestResourceStream(resource);
            StreamReader streamReader = new StreamReader(stream);
            String ret = streamReader.ReadToEnd();
            return ret;
        }
 

Obtener el espacio libre del las unidades del sistema

 
        Dim diskClass As New System.Management.ManagementClass("Win32_LogicalDisk")
        Dim disks As System.Management.ManagementObjectCollection = diskClass.GetInstances()
        Dim disk As System.Management.ManagementObject
        Dim space As System.UInt64 = 0
        For Each disk In disks
            If disk("Description").ToString = "Local Fixed Disk" Then
                If disk("Description").ToString = "Local Fixed Disk" Then
                space += CType(disk("FreeSpace"), System.UInt64)
            End If
        Next disk
 

Ejecutar shell y capturar la salida

Ejecutar shell y capturar la salida

 
        Dim p As New Process
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.Arguments = "x.txt y.txt"
        p.StartInfo.WorkingDirectory = "E:\winnt\system32"
        p.StartInfo.FileName = "xcopy "
        p.Start()
        Dim sr As IO.StreamReader = p.StandardOutput
        Dim sb As New System.Text.StringBuilder("")
        Dim sinput As String = ""
        Do Until sinput = "-1"
            sb.Append(sr.ReadLine() & ControlChars.CrLf)
            sinput = sr.Read
        Loop
        Return sb.ToString
 
Proyecto: Juanjo Navarro, 2006 - Diseño: Albin