GRChandrashekar
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(sourcep ath, "*.*", SearchOption.AllDirectorie s);
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
const string sourcepath = @"\\BANGALORECLUB\UPDATES\
readonly string destinationpath = Application.StartupPath;
string[] filePaths;
filePaths = Directory.GetFiles(sourcep
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
}
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.
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);
}
}
}
}
}
}
sorry missed Path.Combine
attached is modified code
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);
}
}
}
}
this link may help you
http://social.msdn.microso