locked
C# move file to a different location RRS feed

  • Question

  • In C# 2008 application, I want to move an excel 2010 file from one directory struture location to another location. I basically want to remove the file from the first location and place the excel file in a second location.

    Thus can you show me code how to accomplish this goal?

    Tuesday, January 8, 2013 5:33 PM

Answers

  • Actually File.Move can not overwrite the existing file. So better way is to use File.Copy to copy the file over the existing file and then delete the file that was copied from orginal location.

    Code sample:

    public static void MoveFile(string sourceFile, string destinationFile)
    {
        MoveFile(sourceFile, destinationFile, false);
    }
    
    public static void MoveFile(string sourceFile, string destinationFile, bool overwrite)
    {
        if (overwrite && File.Exists(destinationFile))
        {
            File.Delete(destinationFile);
        }
        File.Move(sourceFile, destinationFile);
        File.Delete(sourceFile);
    }

    <object height="0" id="plugin0" style=";z-index:1000;" type="application/x-dgnria" width="0"><param name="tabId" value="ff-tab-31" /><param name="counter" value="85" /></object>

    Any fool can know. The point is to understand.(Albert Einstein)

    Tuesday, January 8, 2013 8:26 PM
  • Actually File.Move can not overwrite the existing file. So better way is to use File.Copy to copy the file over the existing file and then delete the file that was copied from orginal location.

    • Edited by MasaSam Tuesday, January 8, 2013 8:04 PM
    • Proposed as answer by Brad Semrad Tuesday, January 8, 2013 9:45 PM
    • Marked as answer by wendy elizabeth Monday, January 14, 2013 3:12 AM
    Tuesday, January 8, 2013 8:02 PM
  • Hi,

    Have you tried http://msdn.microsoft.com/en-us/library/system.io.file.move(v=vs.80).aspx ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Tuesday, January 8, 2013 5:45 PM
  • Actually File.Move can not overwrite the existing file. So better way is to use File.Copy to copy the file over the existing file and then delete the file that was copied from orginal location.

    Code sample:

    public static void MoveFile(string sourceFile, string destinationFile)
    {
        MoveFile(sourceFile, destinationFile, false);
    }
    
    public static void MoveFile(string sourceFile, string destinationFile, bool overwrite)
    {
        if (overwrite && File.Exists(destinationFile))
        {
            File.Delete(destinationFile);
        }
        File.Move(sourceFile, destinationFile);
        File.Delete(sourceFile);
    }


    Right but  why add the extra delete logic on the destination file when the File.Copy can handle it? You need to write extra code when its not necessary.

    As MasaSam proposed:

    File.Copy(source, destination, true);
    
    File.Delete(source);

    Thanks,

    Brad

    Tuesday, January 8, 2013 9:49 PM

All replies

  • Hi,

    Have you tried http://msdn.microsoft.com/en-us/library/system.io.file.move(v=vs.80).aspx ?


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Tuesday, January 8, 2013 5:45 PM
  • Actually File.Move can not overwrite the existing file. So better way is to use File.Copy to copy the file over the existing file and then delete the file that was copied from orginal location.

    • Edited by MasaSam Tuesday, January 8, 2013 8:04 PM
    • Proposed as answer by Brad Semrad Tuesday, January 8, 2013 9:45 PM
    • Marked as answer by wendy elizabeth Monday, January 14, 2013 3:12 AM
    Tuesday, January 8, 2013 8:02 PM
  • Actually File.Move can not overwrite the existing file. So better way is to use File.Copy to copy the file over the existing file and then delete the file that was copied from orginal location.

    Code sample:

    public static void MoveFile(string sourceFile, string destinationFile)
    {
        MoveFile(sourceFile, destinationFile, false);
    }
    
    public static void MoveFile(string sourceFile, string destinationFile, bool overwrite)
    {
        if (overwrite && File.Exists(destinationFile))
        {
            File.Delete(destinationFile);
        }
        File.Move(sourceFile, destinationFile);
        File.Delete(sourceFile);
    }

    <object height="0" id="plugin0" style=";z-index:1000;" type="application/x-dgnria" width="0"><param name="tabId" value="ff-tab-31" /><param name="counter" value="85" /></object>

    Any fool can know. The point is to understand.(Albert Einstein)

    Tuesday, January 8, 2013 8:26 PM
  • Actually File.Move can not overwrite the existing file. So better way is to use File.Copy to copy the file over the existing file and then delete the file that was copied from orginal location.

    Code sample:

    public static void MoveFile(string sourceFile, string destinationFile)
    {
        MoveFile(sourceFile, destinationFile, false);
    }
    
    public static void MoveFile(string sourceFile, string destinationFile, bool overwrite)
    {
        if (overwrite && File.Exists(destinationFile))
        {
            File.Delete(destinationFile);
        }
        File.Move(sourceFile, destinationFile);
        File.Delete(sourceFile);
    }


    Right but  why add the extra delete logic on the destination file when the File.Copy can handle it? You need to write extra code when its not necessary.

    As MasaSam proposed:

    File.Copy(source, destination, true);
    
    File.Delete(source);

    Thanks,

    Brad

    Tuesday, January 8, 2013 9:49 PM
  • This will also work, but is much more compilcated than just using File.Copy first and then File.Delete.

    Wednesday, January 9, 2013 9:06 AM