|
|
In this post, I will show you how to use the File class to quickly replace the contents of a Text File. Check the contents of the Sample.txt file given below.
We will replace the @ with [attherate] and also list the emails one after the other separated by a semicolon(;). Here’s the code to do so:
C#
try
{
string replaceString = string.Join(";",
File.ReadAllLines("D:\\sample.txt")
).Replace("@", "[attherate]");
File.WriteAllText("D:\\sample.txt", replaceString);
Console.WriteLine("Done");
Console.ReadLine();
}
catch (Exception ex)
{
// handle ex
}
VB.NET
Try
Dim replaceString As String = String.Join(";", _
File.ReadAllLines("D:\sample.txt")).Replace("@", "[attherate]")
File.WriteAllText("D:\sample.txt", replaceString)
Console.WriteLine("Done")
Console.ReadLine()
Catch ex As Exception
' handle ex
End Try
After running the code, the output is as shown below
'Like' us on our FaceBook page if you find this blog useful. Thanks!
Did you like this post?
|
|
|
||
|
|
|
|
Save on Delicious |
|
|
subscribe via rss |
|
subscribe via e-mail |
|
|
print this post |
|
follow me on twitter |




comments
1 Response to "Quickly Replace the Contents of a Text File using C# or VB.NET"Very helpful and easy build.
Post a Comment