Sunday, November 13, 2016

MOVE FILES : VB.NET



Imports System
Imports System.IO
Imports System.Text

Public Class Form1

    Public Sub trans()

        Dim fromloc As String = txtfrom.Text & "\" & txtfilename.Text
        Dim toloc As String = txtto.Text & "\" & txtfilename.Text

        Try
            If File.Exists(fromloc) = False Then
                Dim fs As FileStream = File.Create(fromloc)
                fs.Close()
            End If


            If File.Exists(toloc) Then
                File.Delete(toloc)
            End If
            ' Move the file.
            File.Move(fromloc, toloc)
            Console.WriteLine("{0} moved to {1}", fromloc, toloc)
            ' See if the original file exists now.
            If File.Exists(fromloc) Then
                Console.WriteLine("The original file still exists, which is unexpected.")
            Else
                Console.WriteLine("The original file no longer exists, which is expected.")
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "transfer")
        End Try
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        trans()

    End Sub
End Class

Thursday, November 10, 2016

SEARCH FILES ; VB.NET


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sear(txtfilename.Text)

    End Sub

    Private Sub sear(ByVal item)
        Try


            Dim Folder As New IO.DirectoryInfo(txtlocation.Text)
            For Each File As IO.FileInfo In Folder.GetFiles(txtfilename.Text & txtfextension.Text, IO.SearchOption.AllDirectories)
                ListBox1.Items.Add(File.FullName)
                Application.DoEvents()
            Next

        Catch ex As Exception
            MessageBox.Show(ex.Message, "search")
        End Try

    End Sub

    
End Class

Saturday, October 29, 2016

Upload and process txt file - using .split "," & vbcrlf







Imports System.Data
Imports System.Data.SqlClient


Public Class Form1
    Dim myconn As SqlConnection
    Dim mycmd As SqlCommand
    Dim mydr As SqlDataReader
    Dim dtable As DataTable
    Dim x() As String
    Dim y() As String
    Dim val1 As String
    Dim val2 As String


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dprocess()


    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim fd As OpenFileDialog = New OpenFileDialog()

        fd.Title = "Open File Dialog"
        fd.InitialDirectory = "C:\"
        fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
        fd.FilterIndex = 2
        fd.RestoreDirectory = True

        If fd.ShowDialog() = DialogResult.OK Then
            txtaddress.Text = fd.FileName
        End If
        '------------------------------------------------------------------------
        Try
            Dim objreader As New System.IO.StreamReader(txtaddress.Text)
            txtloaded.Text = objreader.ReadToEnd
            objreader.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

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

  
    Public Sub dview()

        Try
            myconn = New SqlConnection(My.Settings.dSetting)
            If myconn.State = System.Data.ConnectionState.Closed Then
                myconn.Open()
            End If

            mycmd = New SqlCommand("select * from table1 ", myconn)
            mydr = mycmd.ExecuteReader()
            'MessageBox.Show("(" & xcounter & ") affected")
            Dim dtable As New DataTable()
            dtable.Load(mydr)

            DataGridView1.DataSource = dtable
            DataGridView1.Refresh()


            myconn.Close()
            mycmd.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "view")
            myconn.Close()
            mycmd.Dispose()
        End Try

    End Sub

    Public Sub dprocess()


        Dim n As Integer = 0
        Dim m As Integer = 0

        Try
            If Len(txtloaded.Text) <> 0 Then
                x = (txtloaded.Text.Split(vbCrLf))
                While x.Length <> n
                    x(n) = x(n).Replace(vbCr, "").Replace(vbLf, "")
                    y = x(n).Split(",")

                    dprocess2()

                    ' MsgBox(x(n))
                    n = n + 1
                End While

            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "process")
        End Try

        Array.Clear(x, 0, x.Length)
        Array.Clear(y, 0, y.Length)


    End Sub

    Public Sub dprocess2()

        dsearch()


        Dim xcounter As Integer = 0
        Try
            myconn = New SqlConnection(My.Settings.dSetting)
            If myconn.State = System.Data.ConnectionState.Closed Then
                myconn.Open()
            End If

            y(1) = Val(y(1)) + Val(val1)
            y(2) = Val(y(2)) + Val(val2)

            mycmd = New SqlCommand("update table1 set val2='" & y(1) & "' , val3='" & y(2) & "'  where val1 ='" & y(0) & "'", myconn)
            xcounter = mycmd.ExecuteNonQuery
            MessageBox.Show("(" & xcounter & ") affected")

            myconn.Close()
            mycmd.Dispose()

            dview()



        Catch ex As Exception
            MessageBox.Show(ex.Message, "update")
            myconn.Close()
            mycmd.Dispose()


        End Try

    End Sub



    Public Sub dsearch()
        Dim i As Integer = 0
        Dim rcount As Integer = 0
        Dim ifound As Boolean = False



        Try
            rcount = DataGridView1.RowCount
            DataGridView1.MultiSelect = False

            While i < rcount And ifound = False
                DataGridView1.Rows(i).Selected = True

                If DataGridView1.SelectedRows(0).Cells(0).Value = y(0) Then

                    val1 = DataGridView1.SelectedRows(0).Cells(1).Value
                    val2 = DataGridView1.SelectedRows(0).Cells(2).Value

                    DataGridView1.CurrentCell = DataGridView1(0, i)

                    ifound = True

                End If

                i = i + 1

            End While


            
        Catch ex As Exception
            MessageBox.Show(ex.Message, "found")
        End Try

        If ifound = False Then MessageBox.Show("no record found", "")
    End Sub

End Class

Monday, July 4, 2016

Filter DataGridView VB.NET

        Try
            Dim dv As New DataView(dtable)
            dv.RowFilter = String.Format("fname like '%{0}%'", TextBox9.Text)
            DataGridView1.DataSource = dv
        Catch ex As Exception
            MessageBox.Show(ex.Message, "filter")
        End Try

Fill ComboBox ; SQL Server ; VB.NET

        Try
            myconn = New SqlConnection(My.Settings.testsettings)
            myconn.Open()
            mycmd = New SqlCommand("select * from test1", myconn)
            dr = mycmd.ExecuteReader

            While dr.Read
                ComboBox1.Items.Add(dr("fname"))

            End While

        Catch ex As Exception
            MessageBox.Show(ex.Message, "cbox")

        End Try

        myconn.Close()
        mycmd.Dispose()
        dr.Close()

Search from DataGridView VB.NET

        Dim i As Integer = 0
        Dim rcount As Integer = 0
        Dim ifound As Boolean = False

        Try
            rcount = DataGridView1.RowCount
            DataGridView1.MultiSelect = False

            While i < rcount And ifound = False
                DataGridView1.Rows(i).Selected = True

                If DataGridView1.SelectedRows(0).Cells(1).Value = TextBox10.Text Then
                    DataGridView1.CurrentCell = DataGridView1(0, i)
                    ifound = True

                else
                    i = i + 1
                End If

                

            End While
        Catch ex As Exception
            MessageBox.Show(ex.Message, "found")
        End Try

        If ifound = False Then MessageBox.Show("no record found", "")

Thursday, June 30, 2016

Some Uses of Windows Management Instrumentation Command-line (WMIC)

(1) Run : Windows CMD(Command Prompt)
(2) Type below command

GET CPU DETAILS
wmic cpu get deviceid, numberofcores,numberoflogicalprocessors


GET MODEL OF REMOTE COMPUTER
* Local PC
wmic computersystem get model, manufacturer
* Network PC
wmic /node:%ipaddress% computersystem get model, manufacturer


GET BIOS SERIAL NO.
* Local PC
wmic bios get serialnumber  
* Network PC
wmic /node:%ipaddress%bios get serialnumber      


FIND INSTALLED PROGRAM 
* Local PC
wmic product get vendor, name
* Network PC
wmic /node:%ipaddress% product get vendor, name


INSTALL REMOTELY
wmic /node:%ipaddress% product call install true,"","rootdirectory\pathtoyour\file.msi"


UNSTALL SOFTWARE ON REMOTEPC's
wmic /node:%ipaddress% product where name=%softwaretobeunstall%" call uninstall


ADD, UPDATE, DELETE FROM Database VB.NET

Imports System.Data

Imports System.Data.SqlClient

ADD
'--------------------------------------------------------------------------------
 Dim xcounter As Integer = 0
        Try
            myconn = New SqlConnection(My.Settings.testsettings)
            myconn.Open()
            mycmd = New SqlCommand("insert into test1 (fname,mname,lname) values ('" & TextBox1.Text & "' , '" & TextBox2.Text & "' , '" & TextBox3.Text & "')", myconn)
            xcounter = mycmd.ExecuteNonQuery
            MessageBox.Show("(" & xcounter & ") affected")
            myconn.Close()
            mycmd.Dispose()
        Catch ex As Exception
            myconn.Close()
            mycmd.Dispose()
            MessageBox.Show(ex.Message)
        End Try
'--------------------------------------------------------------------------------


EDIT
'--------------------------------------------------------------------------------
Dim xcounter As Integer = 0
        Try
            myconn = New SqlConnection(My.Settings.testsettings)
            If myconn.State = System.Data.ConnectionState.Closed Then
                myconn.Open()
            End If

            mycmd = New SqlCommand("update test1 set fname='" & TextBox4.Text & "' , mname='" & TextBox5.Text & "' , lname='" & TextBox6.Text & "' where id ='" & idd & "'", myconn)
            xcounter = mycmd.ExecuteNonQuery
            MessageBox.Show("(" & xcounter & ") affected")

            myconn.Close()
            mycmd.Dispose()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "asdf")
            myconn.Close()
            mycmd.Dispose()


        End Try
'--------------------------------------------------------------------------------


DELETE
'--------------------------------------------------------------------------------
        If MessageBox.Show("Are your sure to delete  '" & DataGridView1.SelectedRows(0).Cells(0).Value & "'", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then


            Dim xcounter As Integer = 0
            Try
                myconn = New SqlConnection(My.Settings.testsettings)
                myconn.Open()
                mycmd = New SqlCommand("delete from test1 where id='" & idd & "'", myconn)
                xcounter = mycmd.ExecuteNonQuery
                MessageBox.Show("(" & xcounter & ") affected")

            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
            myconn.Close()
            mycmd.Dispose()
            gview()

        End If
'--------------------------------------------------------------------------------

Sunday, June 26, 2016

Read and Save .TXT file - VB.Net


READ .TXT File
'TextBox7.Text - browsed file to load
'TextBox8.Text - display
'------------------------------------------
         Try
            Dim objreader As New System.IO.StreamReader(TextBox7.Text)
            TextBox8.Text = objreader.ReadToEnd
            objreader.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
'------------------------------------------


SAVE .TXT File
'TextBox7.Text - browsed file to save as 
'TextBox8.Text - display
'------------------------------------------
        Try
            Dim objwrite As New System.IO.StreamWriter(TextBox7.Text)
            objwrite.Write(TextBox8.Text)
            objwrite.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
'------------------------------------------

Friday, June 24, 2016

Restart Windows Services VB.NET

Restart Windows Services VB.NET

Imports System
Imports System.ServiceProcess
Imports System.IO
Imports System.Threading
'------------------------------------------------------------------------------------

Public Class Form1
    Dim x As Integer

    '------------------------------------------------------------------------------------


    Public Shared Function RestartService(ByVal ServiceName As String) As Boolean
        Dim mySC As ServiceProcess.ServiceController
        mySC = New ServiceProcess.ServiceController(ServiceName)

        If mySC.Status.ToString = "Running" Then
            Form1.Label1.Text = "STOPPING SERVICES"
            mySC.Stop()
            Form1.x = 5
            Form1.Timer1.Enabled = True
        End If

        If mySC.Status.ToString = "Stopped" Then
            Form1.Label1.Text = "RUNNING SERVICES"
            mySC.Start()
            Form1.x = 5
            Form1.Timer2.Enabled = True
        End If

    End Function

    '------------------------------------------------------------------------------------

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = ""
        Label2.Text = ""
        RestartService(TextBox1.Text)
    End Sub

    '------------------------------------------------------------------------------------

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim mySC As ServiceProcess.ServiceController
        mySC = New ServiceProcess.ServiceController(TextBox1.Text)

        If x = 0 Then
            If mySC.Status.ToString = "Stopped" Then

                Label1.Text = "RERUNNING SERVICES"
                Label2.Text = ""
                mySC.Start()
                Timer1.Enabled = False
                x = 5
                Timer2.Enabled = True
            Else
                x = 5
                Timer1.Enabled = True
            End If

        Else
            x = x - 1
            Label2.Text = "TRYING. . . ." & " " & x
            TextBox2.Text = (mySC.Status.ToString())

        End If
    End Sub

    '------------------------------------------------------------------------------------

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim mySC As ServiceProcess.ServiceController
        mySC = New ServiceProcess.ServiceController(TextBox1.Text)

        If x = 0 Then
            If mySC.Status.ToString = "Running" Then
                Label1.Text = "DONE"
                Label2.Text = ""
                Timer2.Enabled = False
            Else
                x = 5
                Timer2.Enabled = True
            End If


        Else
            x = x - 1
            Label2.Text = "TRYING. . . ." & " " & x
            TextBox2.Text = (mySC.Status.ToString())

        End If
    End Sub

    '------------------------------------------------------------------------------------


End Class