This example shows how to use WMI to make a folder compress its contents to save

Started by nandagopal, Dec 03, 2008, 07:33 PM

Previous topic - Next topic

nandagopal

When you click the Compress button, the following code executes. It creates the WMI service object and executes a query similar to:

    Select * From Win32_Directory Where Name = 'C:\\whatever\\dir'

Note that the query uses doubled back slashes because WMI scripts use back slashes as special characters.

The code loops through the returned folders (there should be one) and calls its Compress method to make the folder compress its contents.


Private Sub btnCompress_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnCompress.Click
    Dim wmi_service As Object = _
        GetObject("winmgmts:\\.\root\cimv2")
    Dim query As String = _
        "Select * From Win32_Directory Where Name = '" & _
        txtFolder.Text & "'"
    query = query.Replace("\", "\\")
    Dim folders As Object = wmi_service.ExecQuery(query)
    Debug.WriteLine(query)

    For Each objFolder As Object In folders
        Debug.WriteLine(objFolder.name)
        objFolder.Compress()
    Next objFolder

    MessageBox.Show("Done", "Done", MessageBoxButtons.OK, _
        MessageBoxIcon.Information)
End Sub