Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

File Copy

I have the following function to copy the file

public static void CopyFile(string source, string destination, string filedirectory,
                                    int bufferSize)
        {
            if (!string.IsNullOrEmpty(filedirectory)) // See if directory exists. If not create it.
            {
                if (!Directory.Exists(filedirectory))
                {
                    Directory.CreateDirectory(filedirectory);
                }
            }
            using (FileStream outputFile = File.OpenWrite(destination))
            {
                using (FileStream inputFile = File.OpenRead(source))
                {
                    var buffer = new byte[bufferSize];
                    int bytesRead;
                    while ((bytesRead = inputFile.Read(buffer, 0, bufferSize)) != 0)
                    {
                        outputFile.Write(buffer, 0, bytesRead);
                    }
                }
            }
        }

Problem:
I have a text file in source and destination. The code is supposed to copy the file from source to destination. But what is happening is, it is appending the text in text file, instead of copying the file. I am surprised. Please help
0
GRChandrashekar
Asked:
GRChandrashekar
  • 3
1 Solution
 
hosneylkCommented:
why not use System.IO.File.Copy?
0
 
hosneylkCommented:
or if you want to stick to this change it like this


FileStream fileStream = new FileStream(@"c:\file.txt", FileMode.Create);

public static void CopyFile(string source, string destination, string filedirectory,
                                    int bufferSize)
        {
            if (!string.IsNullOrEmpty(filedirectory)) // See if directory exists. If not create it.
            {
                if (!Directory.Exists(filedirectory))
                {
                    Directory.CreateDirectory(filedirectory);
                }
            }
            using (FileStream outputFile = new FileStream(destination, FileMode.Create))
            {
                using (FileStream inputFile = File.OpenRead(source))
                {
                    var buffer = new byte[bufferSize];
                    int bytesRead;
                    while ((bytesRead = inputFile.Read(buffer, 0, bufferSize)) != 0)
                    {
                        outputFile.Write(buffer, 0, bytesRead);
                    }
                }
            }
        }

Open in new window

0
 
GRChandrashekarAuthor Commented:
Well textfile is only example !

How do i alter this code to always create mode [overwrite if exist] so that all the files being copied do not come across same issue
0
 
hosneylkCommented:
did you try the code above? ignore the first line..
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now