Different way to copy a file
-
In code, how can I copy a file from X:\ to X:\archive without reading entire file contents? I am using :
FileStream fs = new FileStream("X:\\file.dat", FileMode.Open, FileAccess.Read);
// Create a byte array of file stream length byte\[\] writeArray = new byte\[fs.Length\]; //Read block of bytes from stream into the byte array fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); // Create a synchronization object that gets // signaled when verification is complete. ManualResetEvent manualEvent = new ManualResetEvent(false); FileStream fStream = new FileStream("X:\\\\archive\\file.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true); // Check that the FileStream was opened asynchronously. Debug.Print("fStream was {0}opened asynchronously.", fStream.IsAsync ? "" : "not "); // Asynchronously write to the file. IAsyncResult asyncResult = fStream.BeginWrite ( writeArray, 0, writeArray.Length, new AsyncCallback(EndWriteCallback), new State(fStream, writeArray, manualEvent) );
This works, but performing
fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length));
essentially copies the file contents back to across the network
-
In code, how can I copy a file from X:\ to X:\archive without reading entire file contents? I am using :
FileStream fs = new FileStream("X:\\file.dat", FileMode.Open, FileAccess.Read);
// Create a byte array of file stream length byte\[\] writeArray = new byte\[fs.Length\]; //Read block of bytes from stream into the byte array fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); // Create a synchronization object that gets // signaled when verification is complete. ManualResetEvent manualEvent = new ManualResetEvent(false); FileStream fStream = new FileStream("X:\\\\archive\\file.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true); // Check that the FileStream was opened asynchronously. Debug.Print("fStream was {0}opened asynchronously.", fStream.IsAsync ? "" : "not "); // Asynchronously write to the file. IAsyncResult asyncResult = fStream.BeginWrite ( writeArray, 0, writeArray.Length, new AsyncCallback(EndWriteCallback), new State(fStream, writeArray, manualEvent) );
This works, but performing
fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length));
essentially copies the file contents back to across the network
-
In code, how can I copy a file from X:\ to X:\archive without reading entire file contents? I am using :
FileStream fs = new FileStream("X:\\file.dat", FileMode.Open, FileAccess.Read);
// Create a byte array of file stream length byte\[\] writeArray = new byte\[fs.Length\]; //Read block of bytes from stream into the byte array fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); // Create a synchronization object that gets // signaled when verification is complete. ManualResetEvent manualEvent = new ManualResetEvent(false); FileStream fStream = new FileStream("X:\\\\archive\\file.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true); // Check that the FileStream was opened asynchronously. Debug.Print("fStream was {0}opened asynchronously.", fStream.IsAsync ? "" : "not "); // Asynchronously write to the file. IAsyncResult asyncResult = fStream.BeginWrite ( writeArray, 0, writeArray.Length, new AsyncCallback(EndWriteCallback), new State(fStream, writeArray, manualEvent) );
This works, but performing
fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length));
essentially copies the file contents back to across the network
What about
File.Move
? If you really want to copy, then something has to read the entire file. -
In code, how can I copy a file from X:\ to X:\archive without reading entire file contents? I am using :
FileStream fs = new FileStream("X:\\file.dat", FileMode.Open, FileAccess.Read);
// Create a byte array of file stream length byte\[\] writeArray = new byte\[fs.Length\]; //Read block of bytes from stream into the byte array fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length)); //Close the File Stream fs.Close(); // Create a synchronization object that gets // signaled when verification is complete. ManualResetEvent manualEvent = new ManualResetEvent(false); FileStream fStream = new FileStream("X:\\\\archive\\file.dat", FileMode.Create, FileAccess.ReadWrite, FileShare.None, 4096, true); // Check that the FileStream was opened asynchronously. Debug.Print("fStream was {0}opened asynchronously.", fStream.IsAsync ? "" : "not "); // Asynchronously write to the file. IAsyncResult asyncResult = fStream.BeginWrite ( writeArray, 0, writeArray.Length, new AsyncCallback(EndWriteCallback), new State(fStream, writeArray, manualEvent) );
This works, but performing
fs.Read(writeArray, 0, System.Convert.ToInt32(fs.Length));
essentially copies the file contents back to across the network
Well, you have to first read it in order to copy it, so I doubt what you're asking is possible.
Cheers, Vikram. (Got my troika of CCCs!)