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. Different way to copy a file

Different way to copy a file

Scheduled Pinned Locked Moved C#
questionsysadmindata-structuresdebugging
4 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.
  • S Offline
    S Offline
    svanwass
    wrote on last edited by
    #1

    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

    T P V 3 Replies Last reply
    0
    • S svanwass

      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

      T Offline
      T Offline
      Tarakeshwar Reddy
      wrote on last edited by
      #2

      Out of curiosity, will File.Copy[^] not work for you? Nevermind, just saw your other thread and you have already mentioned about the delay in File.Copy

      1 Reply Last reply
      0
      • S svanwass

        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

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        What about File.Move? If you really want to copy, then something has to read the entire file.

        1 Reply Last reply
        0
        • S svanwass

          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

          V Offline
          V Offline
          Vikram A Punathambekar
          wrote on last edited by
          #4

          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!)

          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