https://github.com/adrianmander/SessionFileLocking/blob/master/SessionFileLocking/Program.cs
I reproduced the problem with a very small code example!
Sample output:
Starting file IO
SFTP Session created
Ending file IO
Starting file IO
Couldn't open the file. Another executable (WinSCP, I'm looking at you) has locked it
(32) The process cannot access the file because it is being used by another process: [\\?\C:\Users\andrew.h\documents\visual studio 2017\Projects\SessionFileLocking\SessionFileLocking\bin\Debug\FileForFileIo_NotSFTP.txt]
Ending file IO
And WinSCP is definitely locking that file:
[code]
C:\Users\andrew.hartline>handle -p WinSCP.exe
Nthandle v4.11 - Handle viewer
Copyright (C) 1997-2017 Mark Russinovich
Sysinternals -
www.sysinternals.com
------------------------------------------------------------------------------
WinSCP.exe pid: 9264 OFFICE\andrew.hartline
3C: File C:\Windows
7C: File C:\Users\andrew.hartline\Documents\Visual Studio 2017\Projects\SessionFileLocking\SessionFileLocking\bin\Debug
A4: File C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.16299.192_none_5d760485a7e0eb41
B4: Section \Windows\Theme2392821440
C4: Section \Sessions\1\Windows\Theme3908030276
304: Section \BaseNamedObjects\__ComCatalogCache__
308: File C:\Windows\SysWOW64\en-US\user32.dll.mui
310: File C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_6.0.16299.192_none_5d760485a7e0eb41
31C: Section \Sessions\1\BaseNamedObjects\windows_shell_global_counters
330: Section \Sessions\1\BaseNamedObjects\NLS_CodePage_437_3_2_0_0
348: Section \BaseNamedObjects\__ComCatalogCache__
34C: File C:\Windows\Registration\R00000000000d.clb
370: File C:\Windows\WinSxS\x86_microsoft.windows.c..-controls.resources_6595b64144ccf1df_6.0.16299.192_en-us_73814d6ad4871d2e
374: File C:\Windows\WinSxS\x86_microsoft.windows.c..-controls.resources_6595b64144ccf1df_6.0.16299.192_en-us_73814d6ad4871d2e\comctl32.dll.mui
388: Section \BaseNamedObjects\windows_shell_global_counters
3E0: File C:\Users\andrew.h\Documents\Visual Studio
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2017\Projects\SessionFileLocking\SessionFileLocking\bin\Debug\FileForFileIo_NotSFTP.txt
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4EC: Section \Sessions\1\BaseNamedObjects\C:*ProgramData*Microsoft*Windows*Caches*cversions.2.ro
4F4: Section \Sessions\1\BaseNamedObjects\C:*ProgramData*Microsoft*Windows*Caches*{6AF0698E-D558-4F6E-9B3C-3716689AF493}.2.ver0x0000000000000002.db
4F8: Section \Sessions\1\BaseNamedObjects\C:*ProgramData*Microsoft*Windows*Caches*cversions.2.ro
4FC: Section \Sessions\1\BaseNamedObjects\C:*ProgramData*Microsoft*Windows*Caches*{DDF571F2-BE98-426D-8288-1A9A39C3FDA2}.2.ver0x0000000000000001.db
54C: Section \Sessions\1\BaseNamedObjects\WinSCPConsoleMapping_25648_12289376_826
550: File C:\Users\andrew.h\AppData\Local\Temp\wscp6430.02BF8098.tmp[code]
Weirdly, it appears to be the product of interaction between WinSCP and AlphaFS!
https://www.nuget.org/packages/AlphaFS/
Here is my packages.config:
<package id="AlphaFS" version="2.1.3" targetFramework="net461" />
<package id="WinSCP" version="5.11.3" targetFramework="net461" />
If I use the regular .NET File API, no problem.