Thread: Copy files across a network

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Copy files across a network

    I have a text file located on a file share on my network. I would like to implement some sort of functionality with C# that copies this file from the network share to a local folder on my pc. Is there some sort of way I could do this?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not familiar with C#, but the general principle is the same whatever the language is:
    Option 1: Open the source file for reading, open the destination file for writing (create new file), then read a chunk of data from the source, and write it to the destination file.
    Option 2: Use some way to execute an external application and use "copy //somemachine/someplace/somefile /localplace/localfile" to copy the file. [Depending on the process of doing this, you may need to prefix the "copy" command with a suitable shell such as "cmd.exe copy ..." - but try without first].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Option 3 (optimal): Use an OS API call to do the copying properly. The manual copy doesn't preserve meta-information, while the copy command is an external command and as such not ideal, not to mention all the issues with command injection of the command line. And does copy even work with UNC paths?

    Use the System.IO.File class for this operation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I agree, using an API is another (sometimes better) option.

    Standard Windows copy can be used with UNC paths, yes - and should preserve meta-data, right?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes. But then, it's just a thin wrapper around the CopyFile API.

    And I'll claim that using an API is always the best option when you actually have two files.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input/Output Files
    By zrsmith2 in forum C Programming
    Replies: 3
    Last Post: 06-15-2008, 10:30 PM
  2. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  3. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  4. copy multiple files to a destination file
    By Bones in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2003, 10:47 AM
  5. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM