开发者

Append a txt file in .Net

I am Creating a txt file in.Net with the help of below code.

StreamWriter writerVer = new StreamWriter(@"c:\Version.txt");                    
writerVer.WriteLine("Version : " + Context.Parameters["ver"]);                  
File.SetAttributes(@"C:\Version.txt", FileAttributes.Hidden);

Then I need to Update the file on the next line by keeping the above code as it is.

This code overrites the entire file.

StreamWriter writer = new StreamWriter(@"c:\Version.txt");
开发者_如何学Pythonwriter.WriteLine("Connection string : "+con);

Regrads,

Sachin K


try this

 StreamWriter writerVer = new StreamWriter(@"c:\Version.txt", true);

There is an overload of the StreamWriter constructor that takes an additional bool parameter to indicate if you want to append or not.


Use the constructor which takes the append boolean parameter to indicate whether to append to the file - http://msdn.microsoft.com/en-us/library/36b035cb.aspx

StreamWriter writer = new StreamWriter("file.txt", true);


Alternatively, you could use the static helper methods: File.AppendAllText or File.AppendText.

Note that the latter actually returns a StreamWriter instance, so you'll need to close it before proceeding otherwise you're relying on the GC to do it for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜