C# Writing a file to the APP_DATA folder and getting bitdefender (antivirus) to scan/delete it : This is the code I use to write my file to the app_data folder:

                        var filename = Server.MapPath("~/App_Data") + "/" thefilename;
                        var ms = new MemoryStream();
                        
                        file.InputStream.CopyTo(ms);
                        file.InputStream.Position = 0;
                        byte[] contents = ms.ToArray();

                        var fileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create,
                        System.IO.FileAccess.ReadWrite);
                        fileStream.Write(contents, 0, contents.Length);
                        
                        fileStream.Close();

This writes the file fine however bitdefender does not delete this file if there is a virus on it unless I go on the IIS and manually try to open/move the file, if I do that then it is instantly deleted.

If I copy and paste the test virus file into the app_data folder directly then bitdefender removes it instantly.

I have tried to use various ways to read/move the file with System.IO.File.Move/Open/ReadAllLines
Yet nothing triggers bit defender to remove the file.

The only thing I got to work was creating a new process to open the file, however I don't want to be doing that on the server and I am looking for a different solution. This is the code that does this:

                            Process cmd = new Process();
                            cmd.StartInfo.FileName = filename;
                            cmd.Start();

A solution with System.IO.File.Open would be best for me in this situation but I cannot figure out why it isn't working.