Solved

File Copy

Posted on 2010-11-29
4
389 Views
Last Modified: 2012-05-10
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
Comment
Question by:GRChandrashekar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 10

Expert Comment

by:hosneylk
ID: 34236773
why not use System.IO.File.Copy?
0
 
LVL 10

Accepted Solution

by:
hosneylk earned 500 total points
ID: 34236790
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
 

Author Comment

by:GRChandrashekar
ID: 34236840
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
 
LVL 10

Expert Comment

by:hosneylk
ID: 34236843
did you try the code above? ignore the first line..
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question