Using FileIOPermissions With StreamWriter

--The following exemplifies writing to a text file in a scenario where
--permissions need to be temporarily altered:

string tempPath = @"c:/file.txt";
if (File.Exists(tempPath))
{
FileIOPermission fio =
new FileIOPermission(FileIOPermissionAccess.Write, tempPath);
fio.AddPathList(FileIOPermissionAccess.Write |
FileIOPermissionAccess.Read, tempPath);

fio.Demand(); --use the .Demand method to obtain permission

StreamReader reader = new StreamReader(tempPath);
string content = reader.ReadToEnd();
reader.Close();

for (int j = 0; j < stringToReplace.Length; j++)
{
content = Regex.Replace(content, stringToReplace[j],
replacementString, RegexOptions.IgnoreCase);
}
StreamWriter writer = new StreamWriter(tempPath);
writer.Write(content); writer.Close();
}