Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. move a file from one directory to another

move a file from one directory to another

Scheduled Pinned Locked Moved C#
debugginghelpquestion
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, I'm trying to move a file from 1 directory to another. But no matter what I try, I keep getting this error at runtime: Cannot create a file when that file already exists. string[] filePaths = Directory.GetFiles(dir1, "*.txt"); Directory.Move(filePaths[i], checkedFiles); All the files in filePaths are in bin\debug\FilesToCheck folder And checkedFiles folder is in bin\debug\checkedFiles What am I doing wrong?

    L 2 Replies Last reply
    0
    • Y Yustme

      Hi, I'm trying to move a file from 1 directory to another. But no matter what I try, I keep getting this error at runtime: Cannot create a file when that file already exists. string[] filePaths = Directory.GetFiles(dir1, "*.txt"); Directory.Move(filePaths[i], checkedFiles); All the files in filePaths are in bin\debug\FilesToCheck folder And checkedFiles folder is in bin\debug\checkedFiles What am I doing wrong?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Make sure checkedFiles ends on a backslash, making it a folder, not a file. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
      All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


      Y 1 Reply Last reply
      0
      • L Luc Pattyn

        Make sure checkedFiles ends on a backslash, making it a folder, not a file. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
        All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


        Y Offline
        Y Offline
        Yustme
        wrote on last edited by
        #3

        It has an ending backslash. The file is made with Path.Combine() And in there i added the ending slash.

        1 Reply Last reply
        0
        • Y Yustme

          Hi, I'm trying to move a file from 1 directory to another. But no matter what I try, I keep getting this error at runtime: Cannot create a file when that file already exists. string[] filePaths = Directory.GetFiles(dir1, "*.txt"); Directory.Move(filePaths[i], checkedFiles); All the files in filePaths are in bin\debug\FilesToCheck folder And checkedFiles folder is in bin\debug\checkedFiles What am I doing wrong?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Please show us the first and last of the filePaths[] elements, and the checkedFiles value, as seen by your app. Why do you use Directory.Move rather than File.Move? :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
          All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


          Y 1 Reply Last reply
          0
          • L Luc Pattyn

            Please show us the first and last of the filePaths[] elements, and the checkedFiles value, as seen by your app. Why do you use Directory.Move rather than File.Move? :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
            All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


            Y Offline
            Y Offline
            Yustme
            wrote on last edited by
            #5

            I tried them both. And both give me the same error at runtime. Here are the 2 elements in the filePaths[]: [0]"C:\\Users\\The User\\Documents\\Visual Studio 2008\\Projects\\ProjectMultiThread\\ProjectMultiThread\\bin\\Debug\\filesToCheck\\634029044974663752.txt" [505]"C:\\Users\\The User\\Documents\\Visual Studio 2008\\Projects\\ProjectMultiThread\\ProjectMultiThread\\bin\\Debug\\filesToCheck\\634029930615165770.txt" The checkedFiles variable: C:\Users\The User\Documents\Visual Studio 2008\Projects\ProjectMultiThread\ProjectMultiThread\bin\Debug\Checked Files\ I tried the 'checked files' with and without a space. Just for the record, I'm trying to move this file from folder filesToCheck: '634029930615165770.txt' To the 'Checked Files' folder. I have used File.Move and: FileInfo fi = new FileInfo(this.filesToCheck);

            modified on Monday, March 1, 2010 5:28 AM

            Y 1 Reply Last reply
            0
            • Y Yustme

              I tried them both. And both give me the same error at runtime. Here are the 2 elements in the filePaths[]: [0]"C:\\Users\\The User\\Documents\\Visual Studio 2008\\Projects\\ProjectMultiThread\\ProjectMultiThread\\bin\\Debug\\filesToCheck\\634029044974663752.txt" [505]"C:\\Users\\The User\\Documents\\Visual Studio 2008\\Projects\\ProjectMultiThread\\ProjectMultiThread\\bin\\Debug\\filesToCheck\\634029930615165770.txt" The checkedFiles variable: C:\Users\The User\Documents\Visual Studio 2008\Projects\ProjectMultiThread\ProjectMultiThread\bin\Debug\Checked Files\ I tried the 'checked files' with and without a space. Just for the record, I'm trying to move this file from folder filesToCheck: '634029930615165770.txt' To the 'Checked Files' folder. I have used File.Move and: FileInfo fi = new FileInfo(this.filesToCheck);

              modified on Monday, March 1, 2010 5:28 AM

              Y Offline
              Y Offline
              Yustme
              wrote on last edited by
              #6

              Luc Pattyn, I fixed the problem. I found this site: http://www.devnewsgroups.net/dotnetframework/t1557-file-move-file-copy-exception.aspx[^] And: Jacky Kwok's reply to someone else's post did the trick. But I don't quite understood what he said. A quote from what I mean: [quote] The "BackUpPath" in both "Copy" and "Move" in your code are directory only. However, the "File.Copy" and "File.Move" require it is filename. [/quote] If I understand this correctly, File.Move wants in both arguments a File name like this: File.Move(file1, file2); Isn't that odd? I'm trying to move a file from one directory to another. But here, you're moving a file to another file?

              L 1 Reply Last reply
              0
              • Y Yustme

                Luc Pattyn, I fixed the problem. I found this site: http://www.devnewsgroups.net/dotnetframework/t1557-file-move-file-copy-exception.aspx[^] And: Jacky Kwok's reply to someone else's post did the trick. But I don't quite understood what he said. A quote from what I mean: [quote] The "BackUpPath" in both "Copy" and "Move" in your code are directory only. However, the "File.Copy" and "File.Move" require it is filename. [/quote] If I understand this correctly, File.Move wants in both arguments a File name like this: File.Move(file1, file2); Isn't that odd? I'm trying to move a file from one directory to another. But here, you're moving a file to another file?

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Yes, File.Move takes two filenames, that is perfectly clear in the documentation. FYI: when both files reside in the same disk partition, the file data is not copied, the file is just renamed, i.e. one or two folder lists get modified. The documentation on Directory.Move is not very clear however; AFAIK it can move an entire folder (when given two folder names), or it moves a single file (when given two file names). :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                Y 2 Replies Last reply
                0
                • L Luc Pattyn

                  Yes, File.Move takes two filenames, that is perfectly clear in the documentation. FYI: when both files reside in the same disk partition, the file data is not copied, the file is just renamed, i.e. one or two folder lists get modified. The documentation on Directory.Move is not very clear however; AFAIK it can move an entire folder (when given two folder names), or it moves a single file (when given two file names). :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                  All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                  Y Offline
                  Y Offline
                  Yustme
                  wrote on last edited by
                  #8

                  So you're saying i have to pass the same file name to the destination argument too?

                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Yes, File.Move takes two filenames, that is perfectly clear in the documentation. FYI: when both files reside in the same disk partition, the file data is not copied, the file is just renamed, i.e. one or two folder lists get modified. The documentation on Directory.Move is not very clear however; AFAIK it can move an entire folder (when given two folder names), or it moves a single file (when given two file names). :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                    I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                    All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                    Y Offline
                    Y Offline
                    Yustme
                    wrote on last edited by
                    #9

                    Hi Luc, It seems that the files which are moved, are somehow messed up. - If i try to open it, it's empty and ms windows is 'asking' me if i want to create a new file. - If i try to delete the file, windows is 'telling' me that the file "is no longer located in the-file-path". I was hoping you could help me out here.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups