Ejecutar código al inicio.

30/06/2004 - 07:33 por Aníbal | Informe spam
Hola,
Cuando mi programa se inicia quiero ejecutar una instancia de la clase
FileSystemWatcher por cada disco rigido que exista en la maquina, establecer
sus propiedades y controlar sus eventos (para cada instancia); pero tengo el
problema que al no saber cuantos HD (o particiones) puede haber en una
maquina, obiamente no puedo saber cuantas variables voy a tener que definir
sino hasta que se ejecute el programa y eso sin contar el problema de los
eventos.

¿Sugerencias?

Preguntas similare

Leer las respuestas

#1 Tristan
30/06/2004 - 13:55 | Informe spam
Prueba algo así:

Imports System.Management
Imports System.IO

Dim wmi As New ManagementObjectSearcher("select * from
Win32_LogicalDisk where DriveType = 3")
Dim unidad As ManagementObject
For Each unidad In wmi.Get()
Dim watcher As New FileSystemWatcher(unidad
("DeviceID").ToString() & "\")
AddHandler watcher.Created, AddressOf watcher_Created
' Lo mismo para Changed, Deleted, ...
watcher.EnableRaisingEvents = True
Next

Private Sub watcher_Created(sender As Object, e As
FileSystemEventArgs)
MsgBox("Creado: " & e.FullPath)
End Sub


No olvides añadir una referencia a System.Management.
Respuesta Responder a este mensaje
#2 Eduardo A. Morcillo [MS MVP VB]
30/06/2004 - 17:32 | Informe spam
¿No es mas facil usar Directory.GetLogicalDrives que complicarse con WMI?

For Each unidad As String In Directory.GetLogicalDrives()
Dim watcher As New FileSystemWatcher(unidad)
AddHandler watcher.Created, AddressOf watcher_Created
watcher.EnableRaisingEvents = True
Next

Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo
Respuesta Responder a este mensaje
#3 Tristan
30/06/2004 - 18:27 | Informe spam
Si, pero con GetLogicalDrives no se puede distinguir si
la unidad es un cd/rom, disquetera, unidad de red, etc...
Si no he entendido mal la pregunta, quiere examinar solo
las unidades de disco duro.

"pero tengo el problema que al no saber cuantos HD (o
particiones) puede haber en una maquina"

Desde luego, si he entendido mal, la solución más
sencilla y eficiente es GetLogicalDrives.
Respuesta Responder a este mensaje
#4 Eduardo A. Morcillo [MS MVP VB]
30/06/2004 - 19:15 | Informe spam
"pero tengo el problema que al no saber cuantos HD (o
particiones) puede haber en una maquina"



Se me habia pasado esa parte ;(

Eduardo A. Morcillo [MS MVP VB]
http://www.mvps.org/emorcillo
Respuesta Responder a este mensaje
#5 Aníbal
30/06/2004 - 23:59 | Informe spam
Hola Tristan,
Gracias por el códgo me funcionó bien; pero hay algo que no entiendo:
Si las instancias de FileSystemWatcher estan definidas dentro de el bucle
For Each,
¿No tendrían que dejar de existir una vez que termina el bucle?


"Tristan" escribió en el mensaje
news:2310801c45e99$263c4db0$
Prueba algo así:

Imports System.Management
Imports System.IO

Dim wmi As New ManagementObjectSearcher("select * from
Win32_LogicalDisk where DriveType = 3")
Dim unidad As ManagementObject
For Each unidad In wmi.Get()
Dim watcher As New FileSystemWatcher(unidad
("DeviceID").ToString() & "\")
AddHandler watcher.Created, AddressOf watcher_Created
' Lo mismo para Changed, Deleted, ...
watcher.EnableRaisingEvents = True
Next

Private Sub watcher_Created(sender As Object, e As
FileSystemEventArgs)
MsgBox("Creado: " & e.FullPath)
End Sub


No olvides añadir una referencia a System.Management.
Respuesta Responder a este mensaje
Ads by Google
Help Hacer una preguntaSiguiente Respuesta Tengo una respuesta
Search Busqueda sugerida