Write out all text files from a folder in C# -
i'm creating program user, can create title , message it, saves text file on desktop. user able read of text files created, instead of string saves messages text file. not specific text file, whole folder of text files.
sorry if kind of messy follow, i'm sort of newbie @ c#.
the code:
class mainclass { public static void main (string[] args) { //variables list<string> usrmsg = new list<string>(); bool alive = true; int choice; string titel; //while loop while (alive == true) { console.clear (); console.writeline ("----menu---"); console.writeline ("[1]save value"); console.writeline ("[2]write out messages"); console.writeline ("[3]clear"); console.writeline ("[4]quit"); console.writeline("[5]search title , write out message"); //exception choice in menu if (!int32.tryparse(console.readline(), out choice)) { // parse failed display message user console.writeline("----menu works numbers 1-5----"); console.writeline ("----press enter continue menu----"); console.readline (); continue; } //menu switch switch (choice) { case 1: console.write ("write title of message: "); titel = console.readline (); console.write ("write in message: "); usrmsg.add (console.readline ()); //creates text file named title system.io.file.writealllines(@"c:\users\desktop" + titel + ".txt", usrmsg); console.writeline ("saved"); console.writeline ("press enter continue"); console.readline (); break; case 2: console.writeline ("----messages have saved:"); try { (int = 0; < usrmsg.count; i++) { console.writeline(usrmsg[i]); } } catch (exception ex) { console.writeline("your book empty" + ex); console.readline(); continue; } console.writeline ("--press enter continue menu--"); console.readline (); break; case 3: usrmsg.clear(); console.writeline ("cleared"); console.writeline ("press enter continue menu"); console.readline (); break; case 4: console.writeline ("quit"); alive = false; break; case 5: console.write("search title: "); try{ string searchmsg = console.readline(); string text = file.readalltext(@"c:\users\desktop" + searchmsg + ".txt"); console.writeline(text); } catch { console.writeline ("the title seek not exist in program"); console.writeline ("press enter continue menu"); console.readline (); continue; } console.writeline("press enter continue menu"); console.readline(); break; //exception in menu default: console.writeline ("---choose number between 1 , 5---"); console.writeline ("---press enter continue menu---"); console.readline (); continue; } } } }
you like:
string[] filedata = directory.enumeratefiles(@"c:\users\desktop", "*.txt") .select(x => file.readalltext(x)).toarray();
this create array of strings contains contents of files @ location specify (desktop - @"c:\users\desktop"
) match given pattern (txt files - "*.txt"
).
Comments
Post a Comment