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!

It could be something to do with the "On-Access" scanning that Kaspersky does. I think that disabling the antivirus doesn't actually stop this process from taking place, you have to go in an manually change that setting somewhere. Either that or it's the network filter driver that they all seem to install nowadays.

Have you tried opening it for read, sleep for 100ms and then change your access to append/write? This will tell you if it's the On-Access scan locking the file or not.

Edited 3 Years Ago by Ketsuekiame

This article has been dead for over six months. Start a new discussion instead.