To copy a file in C#, you can use the File.Copy
method from the System.IO
namespace. Here is an example:
main.cs259 chars9 lines
In this example, we first specify the paths for the source and destination files. Then, we call the File.Copy
method and pass in the source file path and the destination file path as arguments. The File.Copy
method will create a new file at the specified destination path and copy the contents of the source file to the new file.
You can also use the FileStream
class to copy the contents of a file. Here's an example:
main.cs502 chars15 lines
In this example, we first open a FileStream
to the source file using the File.Open
method. We then create a new FileStream
for the destination file using the File.Create
method. Finally, we copy the contents of the source file to the destination file using the CopyTo
method of the FileStream
class. We wrap both FileStream
objects in a using
statement to ensure that they are properly closed and disposed of when we're done with them.
gistlibby LogSnag