3

I want to copy a file from one folder to another folder using filestream.How this can be achived.when I try to use file.copy I was getting this file is using by another process, to avoid this I want to use file stream using c#. Can some one provide a sample for copying a file from one folder to another.

N.RaviKumar
  • 275
  • 1
  • 5
  • 14
  • 6
    Possible duplicate of [Is FileStream the safest way to copy a file vs. File.Copy?](http://stackoverflow.com/questions/4959349/is-filestream-the-safest-way-to-copy-a-file-vs-file-copy) – rbr94 Sep 20 '16 at 10:22
  • Possible duplicate of [Using FileStream classes to copy files, why does output not match input?](http://stackoverflow.com/questions/3427185/using-filestream-classes-to-copy-files-why-does-output-not-match-input) – JeetDaloneboy Sep 20 '16 at 10:41
  • Please look this link http://stackoverflow.com/questions/1246899/file-copy-vs-manual-filestream-write-for-copying-file – vijesh Sep 20 '16 at 12:37
  • 2
    You probably need to go back and figure out why you got the *this file is using by another process* error in the first place, rather than throwing another solution at the problem. – sstan Sep 20 '16 at 12:37

3 Answers3

14

for copying i used below code :-

 public static void Copy(string inputFilePath, string outputFilePath)
    {
        int bufferSize = 1024 * 1024;

        using (FileStream fileStream = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.Write,FileShare.ReadWrite))
        //using (FileStream fs = File.Open(<file-path>, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            FileStream fs = new FileStream(inputFilePath, FileMode.Open, FileAccess.ReadWrite);
            fileStream.SetLength(fs.Length);
            int bytesRead = -1;
            byte[] bytes = new byte[bufferSize];

            while ((bytesRead = fs.Read(bytes, 0, bufferSize)) > 0)
            {
                fileStream.Write(bytes, 0, bytesRead);
            }
        }
    }
N.RaviKumar
  • 275
  • 1
  • 5
  • 14
0

You can use Stream.CopyTo method to copy the file like below:

public static string CopyFileStream(string outputDirectory, string inputFilePath)
{
    FileInfo inputFile = new FileInfo(inputFilePath);
    using (FileStream originalFileStream = inputFile.OpenRead())
    {
        var fileName = Path.GetFileName(inputFile.FullName);
        var outputFileName = Path.Combine(outputDirectory, fileName);
        using (FileStream outputFileStream = File.Create(outputFileName))
        {
            originalFileStream.CopyTo(outputFileStream);
        }
        return outputFileName;
    }
}
Gangula
  • 5,193
  • 4
  • 30
  • 59
-2
    string fileName = "Mytest.txt";
    string sourcePath = @"C:\MyTestPath";
    string targetPath =  @"C:\MyTestTarget";
    string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
    string destFile = System.IO.Path.Combine(targetPath, fileName);

    {
        System.IO.Directory.CreateDirectory(targetPath);
    }

    // To copy a file to another location and 
    // overwrite the destination file if it already exists.
    System.IO.File.Copy(sourceFile, destFile, true);
vijesh
  • 1,115
  • 1
  • 11
  • 27