How to read a text file into a string variable

Discussion in 'ASP .Net' started by ad, Feb 23, 2006.

  1. ad

    ad Guest

    I have a text file in the directory of my web application.
    How can I read this text file into a string vaiable?
     
    ad, Feb 23, 2006
    #1
    1. Advertising

  2. StreamReader SR;
    string S;
    SR=File.OpenText(filename);
    S=SR.ReadLine();
    while(S!=null) {
    Console.WriteLine(S);
    S=SR.ReadLine();
    }
    SR.Close();

    --
    Regards

    John Timney
    Microsoft MVP

    "ad" <> wrote in message
    news:...
    >I have a text file in the directory of my web application.
    > How can I read this text file into a string vaiable?
    >
     
    John Timney \( MVP \), Feb 23, 2006
    #2
    1. Advertising

  3. public string ReadFromFile(string filename)
    {
    StreamReader sr = File.OpenText(filename);

    StringBuilder sb = new StringBuilder();
    string str = sr.ReadLine();
    while( str != null )
    {
    sb.Append(str + "\n");
    str = sr.ReadLine();
    }

    sr.Close();
    return sb.ToString();
    }


    Daniel Fisher(lennybacon) | Software Engineer | newtelligenceR AG
    blog: http://staff.newtelligence.net/danielf
    usergroup: http://vfl-niederrhein.net


    -----Original Message-----
    From: ad [mailto:]
    Posted At: Thursday, February 23, 2006 1:28 PM
    Posted To: microsoft.public.dotnet.framework.aspnet
    Conversation: How to read a text file into a string variable
    Subject: How to read a text file into a string variable

    I have a text file in the directory of my web application.
    How can I read this text file into a string vaiable?
     
    Daniel Fisher\(lennybacon\), Feb 23, 2006
    #3
  4. ad

    Hans Kesting Guest

    Re: =?UTF-8?B?SG93IHRvIHJlYWQgYSB0ZXh0IGZpbGUgaW50byBhIHN0cmluZyB2?==?UTF-8?B?YXJpYWJsZQ==?=

    > public string ReadFromFile(string filename)
    > {
    > StreamReader sr = File.OpenText(filename);
    >
    > StringBuilder sb = new StringBuilder();
    > string str = sr.ReadLine();
    > while( str != null )
    > {
    > sb.Append(str + "\n");
    > str = sr.ReadLine();
    > }
    >
    > sr.Close();
    > return sb.ToString();
    > }
    >


    or use ReadToEnd() :

    string str;
    using (StreamReader sr = File.OpenText(filename))
    { str = sr.ReadToEnd(); }


    Hans Kesting
     
    Hans Kesting, Feb 23, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krish
    Replies:
    1
    Views:
    1,107
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 20, 2005
  2. Jason Heyes

    Read text file into std::string.

    Jason Heyes, Jun 9, 2005, in forum: C++
    Replies:
    23
    Views:
    10,760
    Jason Heyes
    Jun 10, 2005
  3. Replies:
    5
    Views:
    294
  4. Alex Dowad
    Replies:
    4
    Views:
    326
    Michel Demazure
    May 1, 2010
  5. Franzl Wisseworst

    Stupid Q: Read complete file into variable or string

    Franzl Wisseworst, Jul 9, 2006, in forum: Perl Misc
    Replies:
    15
    Views:
    242
    Dr.Ruud
    Jul 10, 2006
Loading...

Share This Page