d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Read A Textfile With C#?
Add Reply New Topic New Poll
Member
Posts: 1,478
Joined: Jul 4 2009
Gold: 0.00
Dec 15 2012 10:54am
Hy
i need a programm in c# which reads a text file (c:\text.txt).
The full text should be saved in a string array and maybe a output from the text.
Someone knows how to program this?
Would be great if someone could help me!
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 15 2012 10:56am
Member
Posts: 1,478
Joined: Jul 4 2009
Gold: 0.00
Dec 15 2012 12:29pm
doesnt rly work.
it would be nice if i have a button
when i click the button the program reads the textfile and after reading it stores the files in a array and enters a messagebox or listbox with the output from the textfile
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Dec 15 2012 05:47pm
what doesn't work? post your code and what the problem is
Member
Posts: 12,953
Joined: Mar 14 2004
Gold: 13,142.01
Dec 15 2012 05:56pm
Quote (FEAR_ASD @ Dec 15 2012 02:29pm)
doesnt rly work.
it would be nice if i have a button
when i click the button the program reads the textfile and after reading it stores the files in a array and enters a messagebox or listbox with the output from the textfile


sounds to me like you are asking us to just spoon feed you with the code. we aren't going to do that. if you know c# then you should be able to figure out how to implement the info on msdn. or at least be able to explain to us what the problem seems like it is..."doesn't work" isn't good enough.

This post was edited by PartyInMyPants on Dec 15 2012 06:05pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Dec 15 2012 06:24pm
pseudo code:
Code
open the file for reading
line by line, append each line to a variable
close the file
spawn a control with that variable as the contents


use the resource linked above to figure out the actual code
while i don't work with c#, it looks like you can just copy and paste that from two sections of carteblanche's link....

This post was edited by Azrad on Dec 15 2012 06:25pm
Member
Posts: 129
Joined: Dec 7 2012
Gold: 0.00
Dec 17 2012 10:51pm
yo, 1 line of code

Code
string[] ass_variable = File.ReadAllLines(ass_path_of_txtfile);




Quote (Azrad @ Dec 15 2012 06:24pm)
pseudo code:
[CODE
spawn a control with that variable as the contents[/CODE]


should be easy now

e/ typo

This post was edited by Kalei on Dec 17 2012 10:52pm
Member
Posts: 33,570
Joined: Mar 3 2007
Gold: 2,090.85
Dec 19 2012 06:36pm
This is the safest way because 'using' blocks release resources of the file by calling Dispose() when the last } is hit.

Code

using (FileStream stream = new FileStream("file.txt", FileMode.Open, FileAccess.Read))
using (StreamReader reader = new StreamReader(stream))
      {
          string line = string.Empty;
          while ((line = reader.ReadLine()) != null)
          {
              Console.WriteLine(line);
          }
      }
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll