In Windows if you are trying to replace a locked (in-use) file, technically it is possible to ask Windows to replace this file after OS restart.
My question: How can I do this programmatically in C#?
In Windows if you are trying to replace a locked (in-use) file, technically it is possible to ask Windows to replace this file after OS restart.
My question: How can I do this programmatically in C#?
It can be done via this approach:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
class Program
{
[Flags ]
enum MoveFileFlags
{
MOVEFILE_REPLACE_EXISTING = 0x00000001,
MOVEFILE_COPY_ALLOWED = 0x00000002,
MOVEFILE_DELAY_UNTIL_REBOOT = 0x00000004,
MOVEFILE_WRITE_THROUGH = 0x00000008,
MOVEFILE_CREATE_HARDLINK = 0x00000010,
MOVEFILE_FAIL_IF_NOT_TRACKABLE = 0x00000020
}
[DllImport ("kernel32.dll" , SetLastError = true , CharSet = CharSet .Unicode)]
static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, MoveFileFlags dwFlags);
static void Main(string [] args)
{
string fileName = @"e:\1.wav" ;
MoveFileEx(fileName, null , MoveFileFlags .MOVEFILE_DELAY_UNTIL_REBOOT);
}
}
}
Thanks for insight to @Jeroen Mostert and @Robert McKee.