Foros > C# (Sharp) ( Limpio y listo para usarse ): COMO SE CREA UN TXT EN EL DISCO C DESDE C#

  1. danielin

    Mensajes [5] - Enviado el Lunes 23 de Marzo de 2009 a las 02:04hs

    NO CE COMO CREAR NI PORSUPUESTO LEER UN TXT DESDE C# COMO LE PUEDO HACER??????????????????????????????????????????????????

  2. superSR17

    Mensajes [5] - Enviado el Jueves 26 de Marzo de 2009 a las 00:55hs

    esto es lo que necesitas

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data;

    namespace ConsStatusInvSAP
    {
    class clsStatusInvSAP
    {

    static StreamWriter sw;

    static void Main(string[] args)
    {

    try
    {
    DateTime fecha = DateTime.Now;
    string sfecha = "";
    sfecha = Convert.ToString(fecha.ToString("yyyyMMdd"));
    string FILE_NAME = "gRUPO" + sfecha + ".txt";
    string Directorio = @"C:Documents and SettingsProfesor4Mis documentos";

    if (File.Exists(Directorio + FILE_NAME))
    {
    sw = File.AppendText(Directorio + FILE_NAME);
    sw.WriteLine();
    sw.WriteLine();
    sw.WriteLine(" --------------------------------------------------------");
    sw.WriteLine(" --------------------------------------------------------");
    sw.WriteLine();
    sw.WriteLine("SE EJECUTA NUEVAMENTE LA APLICACION");
    sw.WriteLine("Inicia programa Extracci?n: " + DateTime.Now);
    sw.WriteLine();
    sw.WriteLine(" --------------------------------------------------------");
    sw.WriteLine();
    }
    else
    {
    sw = File.CreateText(Directorio + FILE_NAME);

    sw.WriteLine("estamos reunidos en viernes a las 9 ");
    sw.WriteLine();
    sw.Write("------------------- ");
    sw.Write(DateTime.Now);
    sw.Write(" ------------------- ");
    sw.WriteLine();
    sw.WriteLine();
    sw.WriteLine("Inicia programa Extracci?n: " + DateTime.Now);
    sw.WriteLine(" --------------------------------------------------------");
    sw.WriteLine();
    }



    }
    catch (Exception Excepcion)
    {
    string error = Excepcion.Message.ToString();
    sw.WriteLine("Catch Main Error -> " + error);
    }
    finally
    {
    sw.WriteLine("Fin programa Extracci?n: " + DateTime.Now);
    sw.Close();
    }

    }
    }
    }

  3. thaNat0s

    Mensajes [8] - Enviado el Sabado 09 de Mayo de 2009 a las 23:18hs

    se usa el namespace

    using System.IO;

    string FILE_NAME = "Documento.txt"; //el nombre del documento
    string Directorio = @"C:"; // la ruta donde se guardara el documento

    System.IO.StreamWriter sw = System.IO.File.CreateText(Directorio + FILE_NAME);

    sw.WriteLine(dtReult.Rows[i][0] + "," + dtReult.Rows[i][1] + "," +dtReult.Rows[i][2]);

    sw.Close();

  4. sammish

    Mensajes [1] - Enviado el Lunes 11 de Mayo de 2009 a las 03:15hs

    Leer un archivo de texto
    El c?digo siguiente utiliza la clase StreamReader para abrir, leer y cerrar el archivo de texto. Se puede pasar la ruta de acceso de un archivo de texto al constructor StreamReader para abrir el archivo autom?ticamente. El m?todo de ReadLine lee cada l?nea de texto y incrementa el puntero de archivo a la l?nea siguiente cuando lee. Cuando el m?todo ReadLine alcanza el final del archivo, devuelve una referencia nula.
    1. Crear un archivo de texto de ejemplo en el bloc de notas. Para ello, siga estos pasos:
    a. Pegue el texto siguiente en Bloc de notas:
    hello world
    b. Guardar el archivo como ejemplo.txt.
    Inicie Microsoft Visual Studio.
    En el men? archivo, elija nuevo y, a continuaci?n, haga clic en proyecto .
    Haga clic en proyectos de Visual C# en tipos de proyecto y, a continuaci?n, haga clic en aplicaci?n de consola en plantillas

    Tenga en cuenta En Visual Studio 2005 o Visual Studio 2008, haga clic en Visual C# en tipos de proyecto y, a continuaci?n, haga clic en aplicaci?n de consola en plantillas.
    Agregue el c?digo siguiente al principio del archivo Class1.cs:
    using System.IO;
    nota en Visual Studio 2005 o Visual Studio 2008, el archivo predeterminado es Program.cs.
    Agregue el c?digo siguiente al m?todo Main :
    String line;
    try
    {
    //Pass the file path and file name to the StreamReader constructor
    StreamReader sr = new StreamReader("C:\Sample.txt");

    //Read the first line of text
    line = sr.ReadLine();

    //Continue to read until you reach end of file
    while (line != null)
    {
    //write the lie to console window
    Console.WriteLine(line);
    //Read the next line
    line = sr.ReadLine();
    }

    //close the file
    sr.Close();
    Console.ReadLine();
    }
    catch(Exception e)
    {
    Console.WriteLine("Exception: " + e.Message);
    }
    finally
    {
    Console.WriteLine("Executing finally block.");
    }
    8.- En el men? Depurar, haga clic en Iniciar para compilar y ejecutar la aplicaci?n. Presione ENTRAR para cerrar la ventana de la consola. La ventana de la consola muestra el contenido del archivo de ejemplo.txt.
    Hello world
    de
    Escribir un archivo de texto (ejemplo 1)
    El c?digo siguiente utiliza la clase StreamWriter para abrir, que se va a escribir y cerrar el archivo de texto. De forma similar a la clase StreamReader , puede pasar la ruta de acceso de un archivo de texto al constructor StreamWriter para abrir el archivo autom?ticamente. El m?todo WriteLine escribe una l?nea completa de texto en el archivo de texto.
    1. Inicie Visual Studio.
    2. En el men? archivo, elija nuevo y, a continuaci?n, haga clic en proyecto.
    3. Haga clic en proyectos de Visual C# en tipos de proyecto y, a continuaci?n, haga clic en aplicaci?n de consola en plantillas.

    Tenga en cuenta En Visual Studio 2005 o Visual Studio 2008, haga clic en Visual C# en tipos de proyecto y, a continuaci?n, haga clic en aplicaci?n de consola CLR en plantillas.
    4. Agregue el c?digo siguiente al principio del archivo Class1.cs:
    using System.IO;
    5. Agregue el c?digo siguiente al m?todo Main :
    6. try
    {

    //Pass the filepath and filename to the StreamWriter Constructor
    StreamWriter sw = new StreamWriter("C:\Test.txt");

    //Write a line of text
    sw.WriteLine("Hello World!!");

    //Write a second line of text
    sw.WriteLine("From the StreamWriter class");

    //Close the file
    sw.Close();
    }
    catch(Exception e)
    {
    Console.WriteLine("Exception: " + e.Message);
    }
    finally
    {
    Console.WriteLine("Executing finally block.");
    }
    7. En el men? Depurar, haga clic en Iniciar para compilar y ejecutar la aplicaci?n. Este c?digo crea un archivo que se denomina Test.txt en la unidad c:. abrir Test.txt en un editor de texto, como el Bloc de notas. Test.txt contiene dos l?neas de texto:
    8. Hello World!!
    9. From the StreamWriter class
    De la clase StreamWriter
    Escribir un archivo de texto (el ejemplo 2)
    El c?digo siguiente utiliza la clase StreamWriter para abrir, que se va a escribir y cerrar el archivo de texto. A diferencia del ejemplo anterior, este c?digo pasa dos par?metros adicionales al constructor. El primer par?metro es la ruta de acceso del archivo y el nombre de archivo del archivo. El segundo par?metro, True , especifica que el archivo se abre en modo de agregar. Si se especifica false para el segundo par?metro, el contenido del archivo se sobrescribir? cada vez que ejecute el c?digo. El tercer par?metro especifica Unicode , de modo que StreamWriter codifica el archivo en formato Unicode. Tambi?n puede especificar los siguientes m?todos de codificaci?n para el tercer par?metro:
    ? ASC11
    ? Unicode
    ? UTF7
    ? UTF8
    El m?todo de amortizaci?n es similar al m?todo WriteLine , excepto que el m?todo Write no incrusta autom?ticamente un retorno de carro o avance de l?nea (CR/LF) combinaci?n de caracteres. Esto es ?til cuando desea escribir un car?cter a la vez.
    1. Inicie Visual Studio.
    2. En el men? archivo , elija nuevo y, a continuaci?n, haga clic en proyecto .
    3. Haga clic en proyectos de Visual C# en tipos de proyecto y, a continuaci?n, haga clic en aplicaci?n de consola en plantillas

    Tenga en cuenta En Visual Studio 2005 o Visual Studio 2008, haga clic en Visual C# en tipos de proyecto y, a continuaci?n, haga clic en aplicaci?n de consola en plantillas
    4. Agregue el c?digo siguiente al principio del archivo Class1.cs :
    5. using System.IO;
    using System.Text;
    nota en Visual Studio 2005 o Visual Studio 2008, el archivo predeterminado es Program.cs.
    6. Agregue el c?digo siguiente al m?todo Main :
    7. Int64 x;

    try
    {
    //Open the File
    StreamWriter sw = new StreamWriter("C:\Test1.txt", true, Encoding.ASCII);

    //Writeout the numbers 1 to 10 on the same line.
    for(x=0; x < 10; x++)
    {
    sw.Write(x);
    }

    //close the file
    sw.Close();
    }
    catch(Exception e)
    {
    Console.WriteLine("Exception: " + e.Message);
    }
    finally
    {
    Console.WriteLine("Executing finally block.");
    }
    8. En el men? Depurar , haga clic en Iniciar para compilar y ejecutar la aplicaci?n. Este c?digo crea un archivo que se denomina Test1.txt en la unidad c:. Test1.txt abrir en un editor de texto, como el Bloc de notas. Test1.txt contiene una sola l?nea de texto:
    0123456789
    Lista completa de c?digo
    Leer un archivo de texto
    //Read a Text File
    using System;
    using System.IO;

    namespace readwriteapp
    {
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {

    String line;

    try
    {
    //Pass the file path and file name to the StreamReader constructor
    StreamReader sr = new StreamReader("C:\Sample.txt");

    //Read the first line of text
    line = sr.ReadLine();

    //Continue to read until you reach end of file
    while (line != null)
    {
    //write the lie to console window
    Console.WriteLine(line);
    //Read the next line
    line = sr.ReadLine();
    }

    //close the file
    sr.Close();
    Console.ReadLine();
    }
    catch(Exception e)
    {
    Console.WriteLine("Exception: " + e.Message);
    }
    finally
    {
    Console.WriteLine("Executing finally block.");
    }
    }
    }
    }
    Escribir un archivo de texto (versi?n 1)
    //Write a text file - Version-1
    using System;
    using System.IO;

    namespace readwriteapp
    {
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    try
    {

    //Pass the filepath and filename to the StreamWriter Constructor
    StreamWriter sw = new StreamWriter("C:\Test.txt");

    //Write a line of text
    sw.WriteLine("Hello World!!");

    //Write a second line of text
    sw.WriteLine("From the StreamWriter class");

    //Close the file
    sw.Close();
    }
    catch(Exception e)
    {
    Console.WriteLine("Exception: " + e.Message);
    }
    finally
    {
    Console.WriteLine("Executing finally block.");
    }
    }
    }
    }
    Escribir un archivo de texto (versi?n 2)
    //Write a text file - Version 2
    using System;
    using System.IO;
    using System.Text;

    namespace readwriteapp
    {
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {

    Int64 x;

    try
    {
    //Open the File
    StreamWriter sw = new StreamWriter("C:\Test1.txt", true, Encoding.ASCII);

    //Writeout the numbers 1 to 10 on the same line.
    for(x=0; x < 10; x++)
    {
    sw.Write(x);
    }

    //close the file
    sw.Close();
    }
    catch(Exception e)
    {
    Console.WriteLine("Exception: " + e.Message);
    }
    finally
    {
    Console.WriteLine("Executing finally block.");
    }
    }
    }
    }

  5. cridigo

    Mensajes [10] - Enviado el Domingo 28 de Junio de 2009 a las 04:55hs

    using System.IO;
    Para abrirlo:


    textBox1.Text = File.ReadAllText(@"C:Archivo.txt");


    Para guardarlo:


    textBox1.Text = File.WriteAllText(@"C:Archivo.txt",textBox1.Text);


    ?


responder

Para poder responder a este tema tenes que ingresar. Si no tenes una cuenta podes registrarse gratis..

publicidad

API DE FACEBOOK
GOOGLE CALENDAR API
GOOGLE MAPS API