Foros del Web » Programación para mayores de 30 ;) » .NET »

Trabajar con System.IO.File

Estas en el tema de Trabajar con System.IO.File en el foro de .NET en Foros del Web. Estoy tratando de crear un archivo, escribirle y luego leerlo. Hice lo siguiente: Código: Dim FS As System.IO.File If FS.Exists("c:\Config.txt") = False Then FS.Create("c:\Config.txt") FS.OpenWrite("c:\Config.txt").BeginWrite("PRUEBA ...
  #1 (permalink)  
Antiguo 24/06/2004, 11:57
Avatar de RsOfT  
Fecha de Ingreso: marzo-2002
Ubicación: InterNET
Mensajes: 1.121
Antigüedad: 22 años, 1 mes
Puntos: 7
Trabajar con System.IO.File

Estoy tratando de crear un archivo, escribirle y luego leerlo.

Hice lo siguiente:

Código:
Dim FS As System.IO.File

If FS.Exists("c:\Config.txt") = False Then
   FS.Create("c:\Config.txt")
   FS.OpenWrite("c:\Config.txt").BeginWrite("PRUEBA DE ESCRITURA")
End If
Pero no se usar el BeginWrite, me piede un paquetón de parámetros. Alguien me podría explicar como leer y escribir utilizando la clase System.IO.File?
__________________
.::RsOfT::.
--El que se aferra a lo conocido, nunca conocerá lo desconocido--
--Es intentando lo imposible como se realiza lo posible--
--Es de pésimo gusto contentarse con algo mediocre cuando lo excelente está a nuestro alcance--
  #2 (permalink)  
Antiguo 24/06/2004, 12:14
Avatar de luiscl  
Fecha de Ingreso: abril-2004
Ubicación: Zaragoza
Mensajes: 305
Antigüedad: 20 años
Puntos: 0
Muy buenas. A ver si te sirve esto:

- Para crear y escribir:

<%@ Import Namespace="System.IO" %>

<Script Runat="Server">

Sub Button_Click( s As Object, e As EventArgs )
Dim objStreamWriter As StreamWriter

objStreamWriter = File.AppendText( MapPath( "myFile.txt" ) )
objStreamWriter.WriteLine( txtInput.Text )
objStreamWriter.Close
End Sub

</Script>

<html>
<head><title>AppendText.aspx</title></head>
<body>
<form runat="Server">

<h1>Append to a Text File:</h1>
<br>
<asp:TextBox
ID="txtInput"
Runat="Server" />

<asp:Button
Text="Append Text!"
OnClick="Button_Click"
Runat="Server" />

</form>
</body>
</html>


- Para leer

<%@ Import Namespace="System.IO" %>

<Script Runat="Server">

Sub Page_Load
Dim objStreamReader As StreamReader
Dim strInput As String

If File.Exists( MapPath( "myFile.txt" ) ) Then
objStreamReader = File.OpenText( MapPath( "myFile.txt" ) )
strInput = objStreamReader.ReadLine()
While strInput <> Nothing
lblContents.Text &= "<li>" & strInput
strInput = objStreamReader.ReadLine()
End While
objStreamReader.Close
Else
lblContents.Text = "myFile.txt does not exist!"
End If
End Sub

</Script>

<html>
<head><title>OpenText.aspx</title></head>
<body>

<h1>Contents of myFile.txt</h1>

<asp:Label
ID="lblContents"
Runat="Server" />

</body>
</html>

Disculpa que te lo dé por separado, pero ando fatal de tiempo. Si tienes cualquier duda, ya sabes.

Un saludo.
__________________
Pide lo que quieras...y luego paga por ello
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:48.