vbdotnet.it

Tricks & tips, workaround, forum and ideas with .Net

About the author

Enrico Rossini è il gestore di questo blog.
E-mail me Send mail

Recent posts

Recent comments

Contributi

Best 6 ~ 6 users ~ 6 comments

Info legali

Le opinioni espresse in questo blog sono strettamente personali e ogni persona è responsabile dei commenti che inserisce. I marchi citati sono delle rispettive aziende.

© Copyright 2010

Advertising


Come conoscere l'ID della scheda madre?

Questa funzione consente di conoscere l’identificativo del processore montato sul PC. Per poter eseguire correttamente questa funzione è necessario importare il namespace System.Management

   1:  Imports System.Management
   2:   
   3:  Public Function GetMotherBoardID() As String
   4:     Dim strMotherBoardID As String = String.Empty
   5:     Dim query As New SelectQuery("Win32_BaseBoard")
   6:     Dim search As New ManagementObjectSearcher(query)
   7:   
   8:     For Each info As ManagementObject In search.Get()
   9:        strMotherBoardID = info("SerialNumber").ToString()
  10:     Next
  11:   
  12:     Return strMotherBoardID
  13:  End Function

È possibile modificare questa funzione e trovare altre informazioni sul sistema.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Sunday, December 28, 2008 10:57 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Come conoscere l'ID del processore?

Questa funzione consente di conoscere l’identificativo del processore montato sul PC. Per poter eseguire correttamente questa funzione è necessario importare il namespace System.Management

   1:  Imports System.Management
   2:   
   3:  Public Function GetProcessorId() As String
   4:     Dim strProcessorId As String = String.Empty
   5:     Dim query As New SelectQuery("Win32_processor")
   6:     Dim search As New ManagementObjectSearcher(query)
   7:   
   8:     For Each info As ManagementObject In search.Get()
   9:        strProcessorId = info("processorId").ToString()
  10:     Next
  11:   
  12:     Return strProcessorId
  13:  End Function

Questa funzione può essere modificata per conoscere altre informazioni sulla macchina.

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Sunday, December 28, 2008 10:54 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Copia ricorsiva di File e Cartelle in VB.Net

Sembra banale, ma a volte la semplice copia dei file e delle sottodirectory contenute in una cartella può mettere in crisi un programmatore, proprio per la sua banalità.
Proprio per evitare questo, posto un esempio di copia ricorsiva.

Private Sub Copia(ByVal PathOrigine As String, _
            ByVal PathDestinazione As String)
    Dim Files() As String

    If PathDestinazione.Chars(PathDestinazione.Length - 1) <>
            Path.DirectorySeparatorChar Then
        PathDestinazione += Path.DirectorySeparatorChar
    End If

    If Not Directory.Exists(PathDestinazione) Then
        Directory.CreateDirectory(PathDestinazione)
    End If

    Files = Directory.GetFileSystemEntries(PathOrigine)

    Dim Elemento As String

    For Each Elemento In Files
        'Sotto cartelle
        If Directory.Exists(Elemento) Then
            Copia(Elemento, PathDestinazione +
                Path.GetFileName(Elemento))
            ' File nella cartella
        Else
            File.Copy(Elemento, PathDestinazione +
                Path.GetFileName(Elemento), True)
        End If
    Next Elemento
End Sub


Currently rated 3.3 by 3 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by davide on Saturday, December 20, 2008 1:19 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Scompattare con SharpZipLib e VB.net un file zip con sottocartelle

Routine di scompattazione di un file Zip con l'utilizzo della dll rilasciata da ICSharpCode (IC#Code), open source, che si chiama SharpZipLib (#ZipLib) scaricabile liberamente QUI


Imports ICSharpCode.SharpZipLib.Zip
......
If File.Exists(fileName) Then

    destPath = fileName.Substring(0, fileName.Length - 4) 

    If Not Directory.Exists(destPath) Then
        Directory.CreateDirectory(destPath)
    Else
        For Each s As String In Directory.GetFiles(destPath)
            File.Delete(s)
        Next
    End If

    Dim inStream As New ZipInputStream(File.OpenRead(fileName))
    Dim outStream As FileStream
    Dim entry As ZipEntry
    Dim buff(2047) As Byte
    Dim bytes As Integer

    Do While True
        Try
            entry = inStream.GetNextEntry()
            If entry Is Nothing Then
                Exit Do
            End If

            If entry.Name.Last() = "/" Then
                Directory.CreateDirectory(destPath & "\" & _
                entry.Name.Replace("/""\"))
            Else
                Try
                    outStream = File.Create(destPath & _
                    "\" & entry.Name, 2048)
                    Do While True
                        bytes = inStream.Read(buff, 0, 2048)
                        If bytes = 0 Then
                            Exit Do
                        End If
                        outStream.Write(buff, 0, bytes)
                    Loop
                    outStream.Close()
                Catch
                End Try
            End If
        Catch
 ex As ZipException
            rtn += ex.Message & vbCrLf
        End Try
    Loop

    inStream.Close()
Else
    rtn = "File '" & fileName & "' non trovato."
End If

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,
Categories: vb.net
Posted by davide on Saturday, December 20, 2008 1:16 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Select Distinct su DataSet

Le funzioni sotto riportate permettono di effettuare da codice una query con "Select Distinct" su un DataSet.

FieldNames è un array di string con in campi su cui fare la select distinct.

 


Public Function SelectDistinct(ByVal SourceTable As DataTable, _
    ByVal ParamArray FieldNames() As StringAs DataTable

    Dim lastValues() As Object
    Dim newTable As DataTable

    If FieldNames Is Nothing OrElse FieldNames.Length = 0 Then
        Throw New ArgumentNullException("FieldNames")
    End If

    lastValues = New Object(FieldNames.Length - 1) {}
    newTable = New DataTable

    For Each field As String In FieldNames
        newTable.Columns.Add(field, SourceTable.Columns(field).DataType)
    Next

    For Each Row As DataRow In SourceTable.Select("",String.Join(", ", FieldNames))
        If Not fieldValuesAreEqual(lastValues, Row, FieldNames)Then
            newTable.Rows.Add(createRowClone(Row, newTable.NewRow(), FieldNames))
            setLastValues(lastValues, Row, FieldNames)
        End If
    Next

    Return newTable
End Function


Private Function fieldValuesAreEqual(ByVal lastValues() As Object, _
    ByVal currentRow As DataRow, ByVal fieldNames() As StringAs Boolean

    Dim areEqual As Boolean = True

    For i As Integer = 0 To fieldNames.Length - 1
        If lastValues(i) Is Nothing OrElse NotlastValues(i).Equals(currentRow(fieldNames(i))) Then
            areEqual = False
            Exit For
        End If
    Next

    Return areEqual
End Function


Private Function createRowClone(ByVal sourceRow As DataRow, _
    ByVal newRow As DataRow, ByVal fieldNames() As StringAsDataRow

    For Each field As String In fieldNames
        newRow(field) = sourceRow(field)
    Next

    Return newRow
End Function


Private Shared Sub setLastValues(ByVal lastValues() As Object, _
    ByVal sourceRow As DataRow, ByVal fieldNames() As String)

    For i As Integer = 0 To fieldNames.Length - 1
        lastValues(i) = sourceRow(fieldNames(i))
    Next
End Sub

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,
Categories: vb.net
Posted by davide on Saturday, December 20, 2008 1:14 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Response.Redirect in UpdatePanel Ajax

A volte può capitare di dover utilizzare la funzioneResponse.Redirect in un UpdatePanel... Spesso, però, questa funzione restituisce un errore. Questo (di solito) significa che in manca una parte della configurazione nel file web.config
Basterà infatti aggiungere

<httpModules>

<add name="ScriptModule"type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

</httpModules>


e tutto funzionerà.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: ,
Categories: asp.net | Work around | ajax
Posted by davide on Saturday, December 20, 2008 1:13 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Accesso ad un file Microsoft Access

Ogni tanto capita di doversi collegare ad un file Microsoft Access. E capita anche di non ricordarsi più come si fa :)

Ricorda inoltre che è necessario sempre importare il namespace System.Data.OleDB.

 

Recuperare dei dati dal file Access

   1:  Try
   2:     cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;")
   3:     ' questo è il provider che si usa quando ci si deve collegare ad un file Microsoft Access
   4:     cn.Open()
   5:     
   6:     cmd = New OleDbCommand("select * from table1", cn)
   7:     dr = cmd.ExecuteReader
   8:     While dr.Read()
   9:        ' carica i dati nei textbox attraverso l'index delle colonne
  10:        ' il numero può essere sostituito dal nome delle colonne
  11:        TextBox1.Text = dr(0)
  12:        TextBox2.Text = dr(1)
  13:        TextBox3.Text = dr(2)
  14:     End While
  15:  Catch
  16:  End Try
  17:   
  18:  ' chiude le connessioni
  19:  dr.Close()
  20:  cn.Close()

 

Inserire un record nel file Access

   1:  Dim cn As OleDbConnection
   2:  Dim cmd As OleDbCommand
   3:  Dim dr As OleDbDataReader
   4:  Dim icount As Integer
   5:  Dim str As String
   6:   
   7:  Try
   8:     cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\emp.mdb;")
   9:     cn.Open()
  10:   
  11:     ' crea la stringa di inserimento. Questa stringa può essere modificata anche per cancellare dei record
  12:     str = "insert into table1 values(" & CInt(TextBox1.Text) & ",'" & TextBox2.Text & "','" & TextBox3.Text & "')"
  13:     cmd = New OleDbCommand(str, cn)
  14:     icount = cmd.ExecuteNonQuery
  15:   
  16:    ' visualizza il numero di record inseriti
  17:     MessageBox.Show(icount)
  18:  Catch
  19:  End Try
  20:   
  21:  cn.Close()

Ecco perché ho scritto questo post... a futura memoria! Ciao!

P. S.: ogni tanto scrivetemi qualche commento :(

Currently rated 5.0 by 4 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Monday, December 15, 2008 7:26 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Controllare l'inserimento di soli numeri in un textbox

Ogni tanto capita che ci sia la necessità di controllare l'inserimento dei caratteri in un textbox. In questo codice andiamo a controllare che i caratteri inseriti siano numeri, la virgola o il tasto di cancellazione. Il codice deve essere inserito nell'evento KeyPress del controllo.

   1:  If (Convert.ToInt16(e.KeyChar) >= 48 And Convert.ToInt16(e.KeyChar) <= 57) Or _
   2:     Convert.ToInt16(e.KeyChar) = 8 Or e.KeyChar = "," Then
   3:     e.KeyChar = e.KeyChar.ToString.ToUpper
   4:  Else
   5:     e.KeyChar = ""
   6:  End If

Ciao a tutti!

Sponsor

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by enrico on Thursday, December 04, 2008 1:13 PM
Permalink | Comments (0) | Post RSSRSS comment feed