Escolar Documentos
Profissional Documentos
Cultura Documentos
net
----------------------------------------------Contenido:
Contar archivos en un directorio
Crear un archivo vaco
Crear un archivo plano y aadirle texto
Listar archivos
ByVal e As System.EventArgs) _
3.
Handles MyBase.Load
4.
5.
Try
6.
7.
8.
9.
ContadorDeArchivos = My.Computer.FileSystem.GetFiles("C:\WINDOWS")
10.
11.
12.
13.
' error
14.
15.
16.
17.
18.
19. End Sub
3.
4.
5.
Try
6.
7.
8.
Archivo = System.IO.File.Create("c:\Prueba.txt")
9.
10.
11.
12.
' error
Catch oe As Exception
MsgBox(oe.Message, MsgBoxStyle.Critical)
End Try
8.
ByVal e As System.EventArgs) _
9.
Handles MyBase.Load
10.
11.
12.
13.
Dim Linea As String = "Lnea de texto " & vbNewLine & "Otra linea de texto"
14.
oSW.WriteLine(Linea)
15.
oSW.Flush()
16.
17.
End Sub
3.
4.
Try
5.
6.
7.
8.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
9.
10.
"c:\windows", _
11.
FileIO.SearchOption.SearchAllSubDirectories, _
12.
"*.dll")
13.
14.
ListBox1.Items.Add(Archivo)
15.
Next
16.
' errores
17.
Catch oe As Exception
18.
MsgBox(oe.Message, MsgBoxStyle.Critical)
19.
End Try
3.
4.
Try
5.
6.
7.
8.
Archivo = My.Computer.FileSystem.FindInFiles( _
9.
"C:\", _
10.
"Hola Mundo", _
11.
True, _
12.
FileIO.SearchOption.SearchTopLevelOnly)
13.
14.
15.
' Agrega
16.
ListBox1.Items.Add(name)
17.
Next
18.
' error
19.
20.
21.
Catch oe As Exception
MsgBox(oe.Message, MsgBoxStyle.Critical)
End Try
22. nd Sub
4.
5. Public Class Form1
6.
7.
8.
ByVal e As System.EventArgs) _
9.
Handles MyBase.Load
10.
11.
' Directory
12.
If Directory.Exists("c:\windows") Then
13.
14.
15.
16.
17.
18.
19.
20.
21.
If File.Exists("c:\windows\explorer.exe") Then
MsgBox("Existe", MsgBoxStyle.Information)
Else
MsgBox("No existe", MsgBoxStyle.Information)
22.
End If
23.
End Sub
7 - Renombrar un archivo
( My.Computer.FileSystem.RenameFile )
Texto planoImprimir
1. Option Explicit On
2.
3. Public Class Form1
4.
5.
6.
7.
8.
9.
10.
11.
Try
12.
13.
My.Computer.FileSystem.RenameFile(sPath, sNuevoNombre)
14.
15.
' errores
16.
17.
18.
19.
20.
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical)
End Try
End Sub
8 - Mover
( My.Computer.FileSystem.MoveFile )
Texto planoImprimir
1. Option Explicit On
2.
3. Public Class Form1
4.
5.
6.
7.
8.
9.
10.
11.
Try
12.
13.
My.Computer.FileSystem.MoveFile(sArchivoOrigen, _
14.
sRutaDestino, _
15.
True)
16.
17.
' errores
18.
19.
20.
21.
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical)
End Try
End Sub
5.
6.
7.
8.
Try
9.
10.
11.
12.
13.
14.
' Archivo
15.
My.Computer.FileSystem.DeleteFile( _
16.
Spath, _
17.
FileIO.UIOption.AllDialogs, _
18.
FileIO.RecycleOption.SendToRecycleBin, _
19.
FileIO.UICancelOption.DoNothing)
20.
21.
' carpeta
22.
My.Computer.FileSystem.DeleteDirectory( _
23.
sdir, _
24.
FileIO.UIOption.AllDialogs, _
25.
FileIO.RecycleOption.SendToRecycleBin, _
26.
FileIO.UICancelOption.DoNothing)
27.
28.
29.
30.
31.
32.
' errores
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical)
End Try
End Sub
5.
6.
7.
8.
Try
9.
10.
11.
12.
13.
vbNewLine & _
14.
sTempFile, _
15.
16.
17.
18.
19.
20.
' errores
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical)
End Try
21.
End Sub
5.
6.
7.
8.
Try
9.
10.
11.
12.
With My.Computer.FileSystem
13.
14.
If .FileExists(SPath) Then
15.
16.
sContent = .ReadAllText(SPath)
17.
18.
Else
19.
20.
End If
21.
End With
22.
' errores
23.
24.
25.
26.
Catch ex As Exception
MsgBox(ex.Message.ToString, MsgBoxStyle.Critical)
End Try
End Sub
9.
10.
11.
12.
13.
' Obtener todos los archivos .exe del directorio windows ( inclyendo subdirecto
rios )
For Each archivos As String In Directory.GetFiles("c:\windows", _
14.
"*.exe", _
15.
SearchOption.AllDirectories)
16.
17.
18.
19.
ListBox1.Items.Add(archivos.ToString)
20.
Next
21.
22.
23.
24.
25.
26.
27.
ListBox2.Items.Add(archivos.ToString)
28.
Next
29.
30.
End Sub
13 - Obtener propiedades
Ejemplo que usa la clase FileInfo de system.IO para obtener algunas propiedades y datos de
archivos ( el nombre , la fecha y la hora de modificacin en formato corto , el tamao en
bytes y la extensin )
Colocar un control Listview, un button y un control TextBox
4. Imports System.IO
5.
6. Public Class Form1
7.
8.
9.
10.
With ListView1
11.
.Columns.Add("Nombre", 150)
12.
13.
14.
.Columns.Add("Extensin", 80)
15.
16.
.View = View.Details
17.
.GridLines = True
18.
End With
19.
20.
TextBox1.Text = "c:\windows"
21.
End Sub
22.
23.
24.
25.
26.
27.
28.
ListView1.Items.Clear()
29.
Try
30.
31.
32.
TextBox1.Text, "*.*", _
33.
SearchOption.TopDirectoryOnly)
34.
35.
36.
37.
38.
39.
40.
41.
42.
With item
43.
44.
45.
46.
Archivo.LastWriteTime.ToShortTimeString)
47.
48.
.SubItems.Add(Archivo.Length.ToString)
49.
50.
51.
.SubItems.Add(Archivo.Extension.ToString)
52.
53.
54.
55.
End With
56.
57.
Next
58.
' errores
59.
Catch ex As Exception
60.
Debug.Print(ex.Message.ToString)
61.
Beep()
62.
End Try
63.
64.
End Sub
Explorar
Copiar
Analizar
Encriptar
Drag drop
Programacin eficaz
La ruta de acceso no es vlida por una de las razones siguientes: es una cadena de
longitud cero, slo contiene un espacio en blanco, contiene caracteres no vlidos o
es una ruta de acceso de dispositivo (empieza por \\.\) (ArgumentException).
Existe una situacin de confianza parcial en la que el usuario no tiene los permisos
necesarios (SecurityException).
Vea tambin
Tareas
Cmo: Obtener la coleccin de archivos de un directorio en Visual Basic
Cmo: Eliminar un directorio en Visual Basic
Referencia
UICancelOption (Enumeracin)
My.Computer.FileSystem (Objeto)
UIOption (Enumeracin)
RecycleOption (Enumeracin)