There are 2 solutions.
The first is to use File.Open with FileMode set to append (the file must exist), and FileShare set to ReadWrite.
The second is to interlock the file access with a critical section (lock) to avoid concurrent access:
System.Object lockThis = new System.Object();
public void createTextFile()
{
lock (lockThis)
{
string path = @"D:\testFile.txt";
if (!File.Exists(path))
{
string textSt = ipaddress+pagename;
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine(textSt );
}
}
else
{
using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine(textSt );
}
}
}
}