| 1 | using System.Diagnostics; |
| 2 | |
| 3 | namespace Cage.GuestClipboard; |
| 4 | |
| 5 | |
| 6 | |
| 7 | internal static class WindowsFilePublication |
| 8 | { |
| 9 | internal static async Task Replace(string stage,string destination,CancellationToken token) |
| 10 | { |
| 11 | var elapsed=Stopwatch.StartNew(); |
| 12 | for(;;) |
| 13 | { |
| 14 | token.ThrowIfCancellationRequested(); |
| 15 | try |
| 16 | { |
| 17 | if(File.Exists(destination))File.Replace(stage,destination,null); |
| 18 | else File.Move(stage,destination); |
| 19 | return; |
| 20 | } |
| 21 | catch(Exception error) when(error is IOException or UnauthorizedAccessException) |
| 22 | { |
| 23 | var code=error.HResult&0xffff; |
| 24 | if(code is not (5 or 32) || elapsed.Elapsed>=TimeSpan.FromSeconds(2))throw; |
| 25 | await Task.Delay(25,token); |
| 26 | } |
| 27 | } |
| 28 | } |
| 29 | } |