2012-04-10 11 views
5

का उपयोग कर एक पाठ दस्तावेज़ का उपयोग करनाकैसे, VB.Net

File.AppendAllText("c:\mytextfile.text", "This is the first line") 
File.AppendAllText("c:\mytextfile.text", "This is the second line") 

मैं कैसे पाठ की दूसरी पंक्ति पहले एक के तहत प्रकट करूँ में नई लाइन पर जाने के लिए के रूप में अगर मैं कुंजी दबाएं? ऐसा करने से यह पहली पंक्ति के ठीक आगे दूसरी पंक्ति डालता है।

उत्तर

7

का उपयोग Environment.NewLine

File.AppendAllText("c:\mytextfile.text", "This is the first line") 
File.AppendAllText("c:\mytextfile.text", Environment.NewLine + "This is the second line") 

या आप इस कॉल यह एक StringBuilder उपयोग करने के लिए जिस तरह से बेहतर है के कई है, तो आप StreamWriter

Using writer As new StreamWriter("mytextfile.text", true) 
    writer.WriteLine("This is the first line") 
    writer.WriteLine("This is the second line") 
End Using 
2

शायद:

File.AppendAllText("c:\mytextfile.text", "This is the first line") 
File.AppendAllText("c:\mytextfile.text", vbCrLf & "This is the second line") 

vbCrLf एक नई पंक्ति के लिए एक निरंतर है।

3

उपयोग कर सकते हैं:

Dim sb as StringBuilder = New StringBuilder() 
sb.AppendLine("This is the first line") 
sb.AppendLine("This is the second line") 
sb.AppendLine("This is the third line") 
.... 
' Just one call to IO subsystem 
File.AppendAllText("c:\mytextfile.text", sb.ToString()) 

आप तो लिखने के लिए वास्तव में कई सारे तार हैं तो आप सब कुछ एक विधि में लपेट सकते हैं।

Private Sub AddTextLine(ByVal sb As StringBuilder, ByVal line as String) 
    sb.AppendLine(line) 
    If sb.Length > 100000 then 
     File.AppendAllText("c:\mytextfile.text", sb.ToString()) 
     sb.Length = 0 
    End If   
End Sub 
+0

इसका मतलब है आकार के आधार पर स्मृति में पूर्ण स्ट्रिंग के साथ काम करना, यह एक समस्या हो सकती है। – Magnus

+0

@ मैग्नस हां लेकिन आसानी से कुछ इंटरमीडिएट कॉल के साथ AppendAllText में संभाला जा सकता है। मेरा अद्यतन उत्तर देखें – Steve