Take the following code snipit:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace filetest
{
class Program
{
static void Main(string[] args)
{
string testString = "Test File";
string testFileName = Guid.NewGuid().ToString() + ".txt";
string storagePath = "Z:";
File.WriteAllText(storagePath + "\\" + testFileName, testString);
string input = File.ReadAllText(storagePath + "\\" + testFileName);
File.Delete(storagePath + "\\" + testFileName);
Console.WriteLine("Complete");
Console.ReadKey();
}
}
}
Where Z: is a mapped drive to another machine or NAS. When running this code, there is an exception on the File.ReadAllText line:
System.IO.IOException: The process cannot access the file 'Z:\4c639f96-12a9-410d-94a0-ffc6392aa85a.txt' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
at System.IO.File.InternalReadAllText(String path, Encoding encoding, Boolean checkHost)
at System.IO.File.ReadAllText(String path)
at Switchvox_VM_Handler.Controllers.FaxController.TestWriteStorageLocation(FaxStorage storage) in D:\tfs2017\Switchvox-VM-Handler\Switchvox-VM-Handler\Controllers\FaxController.cs:line 327
Additionally. that file while it can be opened in Notepad, cannot be deleted until the machine running this code is REBOOTED. Ending the process is not enough to release the lock. The lock stays in place forever.
I have witnessed this with .NET Framework 4.7, 4.72, 4.81. The same code does not cause this exception when reworked to .NET 6.
To me this seems like a bug with legacy .NET framework, but maybe I'm missing something. I've simplified it down to the bare minimum for this SO question and verified it still throws the same exception even as a 10 line console project. I have spent time breaking out the writing to the traditional way of using using streamwriter statements, etc, but same results. Also if I replace the File.WriteAllText with a File.Copy command and copy an existing text file to the same location, that file is left locked in the same manner.
Finally if that drive letter Z: is swapped with a local drive letter, i.e. C: it works perfectly fine as one would expect. It only seems to be an issue with network shares (SMB/CIFS).
Any thoughts on how to get around this as I'm not really ready to re-write this project in .NET 6 (core) yet.