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. File.Copy Issue

File.Copy Issue

Scheduled Pinned Locked Moved C#
7 Posts 4 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.
  • B Offline
    B Offline
    BlitzPackage
    wrote on last edited by
    #1

    Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz

    L D 0 B 4 Replies Last reply
    0
    • B BlitzPackage

      Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz

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

      Hi, Windows does not create the directory if it does not already exist. Windows Explorer will create directories for you, the programming interfaces don't. So you have to take care of that yourself using Directory.CreateDirectory. BTW: there is no need to test, creating a directory that already exists is harmless. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


      B 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, Windows does not create the directory if it does not already exist. Windows Explorer will create directories for you, the programming interfaces don't. So you have to take care of that yourself using Directory.CreateDirectory. BTW: there is no need to test, creating a directory that already exists is harmless. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        - before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets


        B Offline
        B Offline
        BlitzPackage
        wrote on last edited by
        #3

        Thanks for your reply. The directory exists, but the file doesn't. Do I need to create the file? The examples in msdn didn't indicate I need to create the file. Plus, how would I create an empty jpg file? Thanks again. Blitz

        1 Reply Last reply
        0
        • B BlitzPackage

          Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz

          D Offline
          D Offline
          dealon
          wrote on last edited by
          #4

          You'd better make sure the destination directory exist while coping file.Hope such code can help:

          string strPath = AppDomain.CurrentDomain.BaseDirectory + @"UserImages\";
          // check the directory existence
          if(!Directory.Exists(strPath))
          Directory.CreateDirectory(strPath);
          //...your file copy code here...
          // eg.
          // File.Copy("d:\\a.txt", strPath + "b.txt");

          Regards, Dealon Impossible is nothing!

          1 Reply Last reply
          0
          • B BlitzPackage

            Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz

            0 Offline
            0 Offline
            0x3c0
            wrote on last edited by
            #5

            Instead of concatenating the path variable together, use System.IO.Path.Combine. That will automatically handle the backslashes. For your problem: as the other replies have stated, the path to the destination folder doesn't exist. You need to create it

            1 Reply Last reply
            0
            • B BlitzPackage

              Good People, I have attempted to use File.Copy to copy from a source file to a new file. However, even after I get the apps running directory (usine AppDomain.CurrentDomain.BaseDirectory + @"UserImages\") it still does not work. It throws an exception claiming it can't find part of the directory of the destination file. What I don't understand is that using AppDomain.CurrentDomain.BaseDirectory + @"Database\AppDatabase.vdb3" to find my database for the application works as expected. Do I have to create the new file first before writing to it? Doesn't File.Copy create the new file? Any ideas? Thanks, Blitz

              B Offline
              B Offline
              BlitzPackage
              wrote on last edited by
              #6

              Thank you for all of your replies. What I don't understand, however, is that the folder does exist. In fact, the path it shows as not being able to find is, in fact, the correct path to the folder. I need it to place the new file in the folder - which already exists. So the directory already exists. I apologize for my confusion, but what am I missing? Thanks, Blitz

              B 1 Reply Last reply
              0
              • B BlitzPackage

                Thank you for all of your replies. What I don't understand, however, is that the folder does exist. In fact, the path it shows as not being able to find is, in fact, the correct path to the folder. I need it to place the new file in the folder - which already exists. So the directory already exists. I apologize for my confusion, but what am I missing? Thanks, Blitz

                B Offline
                B Offline
                BlitzPackage
                wrote on last edited by
                #7

                OMG!!! You all are so right! Thanks. Indeed, it did not exist. See, my error was that I added the "UserImages" folder to my project. However, it was not in my output directory. I failed to realize the difference. Now I know, and knowing is half the battle :-) Thanks so much for your help.

                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