La Web del Programador: Comunidad de Programadores
 
    Pregunta:  56678 - "EL PROCESO NO PUEDE OBTENER ACCESO AL ARCHIVO "
Autor:  Esther
Buenas!!

Tengo un problema al intentar borrar un fichero. Si lo abro al darle al boton con el codigo que hay más abajo y luego trato de borrar el archivo me da este mensaje:
"El proceso no puede obtener acceso al archivo "C:AreadocTarea_11prob.txt" porque está siendo utilizado en otro proceso."

¿Hay alguna forma de hacer que ese fichero no se quede colgado en memoria.?

La verdad es que ya no se que más mirar.

Este es el codigo:
If lblRuta.Text <> "" Then
'Dim f As System.IO.FileStream = New System.IO.FileStream(Me.lblRuta.Text, IO.FileMode.Open)
Dim f As System.IO.FileStream = New System.IO.FileStream(Me.lblRuta.Text, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)
Dim b(f.Length) As Byte
Dim m As System.IO.MemoryStream = New System.IO.MemoryStream(b)
f.Read(b, 0, b.Length)
HttpContext.Current.Response.ClearContent()
HttpContext.Current.Response.ClearHeaders()
HttpContext.Current.Response.ContentType = "application/" & System.IO.Path.GetExtension(Me.lblRuta.Text)
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" & System.IO.Path.GetFileName(Me.lblRuta.Text))
Dim i As Int16
Dim nombre As String
i = lblRuta.Text.LastIndexOf("")
nombre = lblRuta.Text.Substring(i + 1)
HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename= " + nombre + "")
'Response.AddH

  Respuesta:  apis
Hola! cuando sale error es que el archivo se encuentra abierto, o fue abierto y no fue cerrado, en tu codigo creo que no veo que lo cierres o lo liberes. Algo asi como
Const fic As String = "E: mpPrueba.txt"
Dim texto As String

Dim sr As New System.IO.StreamReader(fic)
texto = sr.ReadToEnd()
sr.Close()

Console.WriteLine(texto)