I realize that File.Move is a thin wrapper around the Win32 MoveFile
API, so what I'm about to describe I think applies to that API. If NTFS
permissions grant you read access to the source file but deny you the
rights to delete the source file, I believe I've discovered -- much to
my surprise -- that File.Move will copy the file to the target but fail
to delete the source. And do this without returning an error (or
equivalently, in managed code, without throwing an exception).
I've included a tiny console app that I used to test. Does anyone know
if this is expected behavior? I've come across no documentation or
comments to suggest that this is correct.
In my application, this behavior will be problematic -- i.e. if the
source cannot be deleted, I need the call to not do anything. I suspect
I'll have to do the copy and delete as separate calls...
using System;
using System.IO;
namespace MoveTester
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length != 2)
throw new ArgumentException("Usage:
MoveTester <sourcefile> <destfile>");
string File1 = args[0];
string File2 = args[1];
Console.WriteLine("Moving file {0} to {1}", File1,
File2);
try {
File.Move(File1, File2);
Console.WriteLine("Done!");
}
catch (Exception E) {
Console.WriteLine(E.ToString());
}
}
}
}