locked
MoveFileEx does not work across network shares RRS feed

  • Question

  • Hi,

    I am trying to implement MoveFileEx in order to move a directory from one network share to another. I orginally tried to do this with MoveFileW, but then I read on the MSDN under MoveFileW that this method does not support moving files and directories across volumes. I then read that you can move files and directories using the MoveFileEx method, as long as you supply the optional parameters.

    However, I have supplied the parameters that should make this possible and yet the files & folders do not move between the different shares. If I supply a local source and local destination, then the move completes successfully.

    Here is my code:

            //*******************************************************************//
            [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName,
               MoveFileFlags dwFlags);
    
            [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
            }
            //*******************************************************************//
    
    MoveFileEx(@"\\?\UNC\FileServer01\SharedFolder\", @"\\?\UNC\FileServer02\Archive\SharedFolder\", MoveFileFlags.MOVEFILE_COPY_ALLOWED | MoveFileFlags.MOVEFILE_REPLACE_EXISTING | MoveFileFlags.MOVEFILE_WRITE_THROUGH);

    The MoveFileEx line above does not work, however if I supply a local path like the one below, it works without any problem:
    MoveFileEx(@"c:\users\user\desktop\share\", @"c:\users\user\desktop\archive\share\", MoveFileFlags.MOVEFILE_COPY_ALLOWED | MoveFileFlags.MOVEFILE_REPLACE_EXISTING | MoveFileFlags.MOVEFILE_WRITE_THROUGH);

    I am at a loss as to why it doesn't work, and any help that anyone can give me would be much appreciated.

    Thanks.

    Monday, April 27, 2009 9:52 AM

Answers

  • MSDN states that lpExistingFileName is "The current name of the file or directory on the local computer. "

    What about copying your folders up to the root of your drive? That will make the path shorter and you'd be able to move the files from there.
    • Edited by codevanced Monday, April 27, 2009 11:52 AM
    • Marked as answer by J-N-B Tuesday, April 28, 2009 4:22 PM
    Monday, April 27, 2009 11:51 AM

All replies

  • How about File.Copy ?

    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Monday, April 27, 2009 10:04 AM
  • Hi and thanks for your reply.

    I did use File.Copy in my first attempt, but the vast majority of the folders that I need to move the path is greater than 260 characters. This then causes the application to throw the PathTooLong exception. So unfortunately File.Copy does not work in this case.
    Monday, April 27, 2009 10:09 AM
  • Try this thread


    http://www.eggheadcafe.com/community/aspnet/2/10030871/pathtoolongexception-work.aspx
    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Monday, April 27, 2009 10:16 AM
  • Yes I have seen that thread before, but unfortunately does not provide any useful information. The only piece that might have been useful is the Delimon software page (http://www.delimon.be/Software.aspx), but unfortunately the link does not seem to work anymore! Any other ideas?

    Monday, April 27, 2009 10:40 AM
  • J-N-B,

    Does it work with a mapped drive?
    Monday, April 27, 2009 10:53 AM
  • No it doesn't unfortunately.  Have tried with both mapped drives and UNC path, but it still does not move anything.
    Monday, April 27, 2009 11:24 AM
  • MSDN states that lpExistingFileName is "The current name of the file or directory on the local computer. "

    What about copying your folders up to the root of your drive? That will make the path shorter and you'd be able to move the files from there.
    • Edited by codevanced Monday, April 27, 2009 11:52 AM
    • Marked as answer by J-N-B Tuesday, April 28, 2009 4:22 PM
    Monday, April 27, 2009 11:51 AM
  • Hmmm... I am guessing that this is the reason then for the method failing. I wonder if there is a way to define the lpExistingFileName as a remote directory...?
    Monday, April 27, 2009 2:29 PM