Link to home
Start Free TrialLog in
Avatar of GRChandrashekar
GRChandrashekarFlag for India

asked on

C# File Copy

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
Avatar of beyazlale
beyazlale
Flag of Türkiye image

ASKER CERTIFIED SOLUTION
Avatar of kris_per
kris_per

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kris_per
kris_per


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

Avatar of Naman Goel
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

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