Solved

File Copy

Posted on 2010-11-29
4
385 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is Thread.Sleep? 5 45
Entity Framework 7 28
Help with C#, MVC, razor. 6 21
Exit the loop 4 31
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now