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
Printable View
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
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
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.
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
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.