locked
How to overrite the file if it already exist? RRS feed

  • Question

  • Hi,

    I when i am trying to overrite the file which is alredyy exist in the directory i am facing below error

    "Can not create the file as it is alredy exist in the path"

    i am using below mentioned code

    String MvDestfile = Dts.Variables["envclrpbackupdir"].Value.ToString() + Dts.Variables["var_date"].Value.ToString()
                                            + "\\" + Dts.Variables["var_FileName"].Value.ToString();
                        System.IO.File.Move(Dts.Variables["var_move_pathFile"].Value.ToString(), MvDestfile);
    

    Please help to resolve this ASAP.

    Thankx in advance

    Wednesday, February 12, 2014 5:39 AM

Answers

  • use file system task for this and set Overwrite destination property to true as shown below


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Wednesday, February 12, 2014 5:45 AM
    Answerer
  • Hello Asma,

    File.Move Method method works across disk volumes, and it does not throw an exception if the source and destination are the same. Note that if you attempt to replace a file by moving a file of the same name into that directory, you get an IOException. You cannot use the Move method to overwrite an existing file.

    Excerpt: http://msdn.microsoft.com/en-us/library/system.io.file.move(v=vs.110).aspx

    You may use DELETE and COPY method instead.

    • Proposed as answer by Sofiya Li Thursday, February 13, 2014 2:43 AM
    • Marked as answer by Saeid Hasani Wednesday, February 19, 2014 9:15 PM
    Wednesday, February 12, 2014 6:58 AM
    Answerer

All replies

  • use file system task for this and set Overwrite destination property to true as shown below


    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

    Wednesday, February 12, 2014 5:45 AM
    Answerer
  • Hi,

    thankx....but i dont wanna use the component insted i need to do through C# code...

    so can you please help me to resolve this through code?

    Wednesday, February 12, 2014 6:45 AM
  • Hello Asma,

    File.Move Method method works across disk volumes, and it does not throw an exception if the source and destination are the same. Note that if you attempt to replace a file by moving a file of the same name into that directory, you get an IOException. You cannot use the Move method to overwrite an existing file.

    Excerpt: http://msdn.microsoft.com/en-us/library/system.io.file.move(v=vs.110).aspx

    You may use DELETE and COPY method instead.

    • Proposed as answer by Sofiya Li Thursday, February 13, 2014 2:43 AM
    • Marked as answer by Saeid Hasani Wednesday, February 19, 2014 9:15 PM
    Wednesday, February 12, 2014 6:58 AM
    Answerer
  • Hi,

    thankx....but i dont wanna use the component insted i need to do through C# code...

    so can you please help me to resolve this through code?

    Hi Asma Thabassum,

    According to your description, since the issue regards .NET Framework and C#. I suggest you post the question in the.NET forums at http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?category=netdevelopment. It is appropriate and more experts will assist you.

    As other post, you can copy, move, and delete files by using C# programming. First, check if the file already exists, if it does, you can delete the file via System.IO.File.Delete. Or copy a file to another location and  overwrite the destination file if it already exists.

    For more information, see: How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)

    Regards,
    Sofiya Li



    Sofiya Li
    TechNet Community Support

    Thursday, February 13, 2014 3:11 AM