I need to write a strings into a text file from C#, each string on a new line...How can I do this?
            Asked
            
        
        
            Active
            
        
            Viewed 1.9k times
        
    4
            
            
        - 
                    Possible duplicate of [Append lines to a file using a StreamWriter](http://stackoverflow.com/questions/7306214/append-lines-to-a-file-using-a-streamwriter) – OmG Jan 19 '17 at 12:35
5 Answers
13
            
            
        You can use File.WriteAllLines:
string[] mystrings = new string[] { "Foo", "Bar", "Baz", "Qux" };
System.IO.File.WriteAllLines("myfile.txt", mystrings);
 
    
    
        dtb
        
- 213,145
- 36
- 401
- 431
- 
                    2+1; can't beat a one-liner! Exception handling not included. Available in .NET 2.0+. http://msdn.microsoft.com/en-us/library/system.io.file.writealllines.aspx – p.campbell Dec 17 '09 at 05:03
4
            
            
        If you wish to append the text lines to the file, use AppendAllText:
string appendText = "This is extra text" + Environment.NewLine;
File.AppendAllText(path, appendText);
 
    
    
        SwDevMan81
        
- 48,814
- 22
- 151
- 184
1
            
            
        How about StreamWriter class? Read more here...
And do not forget about exception handling e.g missing file permissions etc.
 
    
    
        Over Killer
        
- 507
- 9
- 24
0
            
            
        You can also use this sample into your code:
string myText = "This is my string";
System.IO.File.WriteAllText(@"C:\Users\Jack\source\repos\My Text.txt", myText);
 
    
    
        Leo
        
- 1
- 2
 
    