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
Solved

C# File Copy

Posted on 2010-11-25
5
1,224 Views
Last Modified: 2012-05-10
I have created a file copy function as follows

const string sourcepath = @"\\BANGALORECLUB\UPDATES\";
        readonly string destinationpath = Application.StartupPath;
        string[] filePaths;

filePaths = Directory.GetFiles(sourcepath, "*.*", SearchOption.AllDirectories);
            const int bufferSize = 32 * 1024;
            foreach (var filelist in filePaths)
            {
                CopyFile(filelist, destinationpath, bufferSize);  
            }



 public static void CopyFile(string source, string destination, int bufferSize)
           
        {
            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);
                    }
                }
            }
        }

Error:
I get access denied error in following line
using (FileStream outputFile = File.OpenWrite(destination))
This is local folder and there is no need of any permission. To ensure what I said is right, i coped the destination path and pasted in my windows explorer where i was able to open the folder and create any files. Not sure what is causing the error
0
Comment
Question by:GRChandrashekar
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:beyazlale
ID: 34212059
0
 
LVL 16

Accepted Solution

by:
kris_per earned 500 total points
ID: 34212073

The 'destination' variable you pass in is directory path; you need to add the file name to it then use it in 'File.OpenWrite'. Corrected code below.
public static void CopyFile(string source, string destination, int bufferSize)
        {
            string destinationFilePath = destination + "\\" + System.IO.Path.GetFileName(source);
            using (FileStream outputFile = File.OpenWrite(destinationFilePath))
            {
                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
 
LVL 16

Expert Comment

by:kris_per
ID: 34212099

If you want to use .net's file copy method (instead of your CopyFile method), then the code would be:
...
foreach (var filelist in filePaths)
{
     string destinationFilePath = destinationpath + "\\" + System.IO.Path.GetFileName(filelist);

     // CopyFile(filelist, destinationpath, bufferSize);   

     System.IO.File.Copy(filelist,destinationFilePath,true);
}

Open in new window

0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34212154
string destinationpath = Application.StartupPath; must be a folder(Directory) and you are trying to access a folder as destination path for writing, that's why it is giving exception.

I had modified the above code in following way and it is working fine.
const string sourcepath = @"\\BANGALORECLUB\UPDATES\";
        readonly string destinationpath = Application.StartupPath;
        string[] filePaths;

filePaths = Directory.GetFiles(sourcepath, "*.*", SearchOption.AllDirectories);
            const int bufferSize = 32 * 1024;
            foreach (var filelist in filePaths)
            {
                CopyFile(filelist, destinationpath+Path.GetFileName(filelist), bufferSize);   
            }



  



 public static void CopyFile(string source, string destination, int bufferSize)
            
        {
            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);
                    }
                }
            }
        }

    }
}

Open in new window

0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34212176
sorry missed Path.Combine

attached is modified code


const string sourcepath = @"\\BANGALORECLUB\UPDATES\";
        readonly string destinationpath = Application.StartupPath;
        string[] filePaths;

filePaths = Directory.GetFiles(sourcepath, "*.*", SearchOption.AllDirectories);
            const int bufferSize = 32 * 1024;
            foreach (var filelist in filePaths)
            {
                CopyFile(filelist, Path.Combine(destinationpath,Path.GetFileName(filelist)), bufferSize);   
            }



  



 public static void CopyFile(string source, string destination, int bufferSize)
            
        {
            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);
                    }
                }
            }
        }

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

829 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