I am having an issue with the following code:
public void appendToLogFile(string logFile, string logText)
{
FileStream fs = null;
try
{
fs = new FileStream(@logFile, FileMode.Append, FileAccess.Write);
{
using (TextWriter logWriter = new StreamWriter(fs))
{
fs = null;
logWriter.WriteLine(logText);
}
}
}
finally
{
if (fs != null)
fs.Dispose();
}
}
Executing using the following command:
appendToLogFile(new DirectoryInfo(genFASTA.outputDirectory).Parent.FullName + "\\454Package.log", logText);
When the above code is executed it fails with the following error:
Exception:Thrown: "Access to the path is denied." (System.UnauthorizedAccessException)
A System.UnauthorizedAccessException was thrown: "Access to the path is denied."
The problem is that this error only occurs when I execute the code on a network drive (no errors encountered when run on a local drive) and the error DOES NOT occur if I change the code to read FileStream(@logFile, FileMode.CreateOpen, FileAccess.ReadWrite);. Whenever this error is encountered in append mode, the file is appended with a single null character to the end.
This error only started happening when we installed Kaspersky AntiVirus however now when we disable protection the error continues to occur but when we uninstall Kaspersky the error stops.
Anyone have an idea how we can append data to files AND keep our antivirus? Any help would be appreciated. Thanks!