J
Jeremy
I am writing a little appliation. One process I want to do is to copy a
folder to a different location, delete the contents of the original
folder, and fill the original location with new files. It's a relatively
simple process, but I've run into a little snag. With Full Control
privileges, I am getting an error stating that I do not have permission
to delete a file in the original location.
I do not believe that this file is in use, because I can successfully
use Directory.Move() without a problem. So I'm beginning to wonder if
there's a problem with my CopyDirectory() method:
public void CopyDirectory(string source, string destination)
{
if (destination[destination.Length - 1] !=
Path.DirectorySeparatorChar)
destination += Path.DirectorySeparatorChar;
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
string[] files = Directory.GetFileSystemEntries(source);
foreach (string fileElement in files)
{
// Subdirectories
if (Directory.Exists(fileElement))
CopyDirectory(fileElement,
destination + Path.GetFileName(fileElement));
// Files in current directory
else
File.Copy(fileElement,
destination + Path.GetFileName(fileElement), true);
}
}
Since Directory.Move() works, I know that I can move the original,
create a new folder of the same name in the original's original
location, and populate the new folder. But since this was my first
thought, I want to find out why I'm getting a permissions error.
Anyone have any thoughts? (I apologize if CopyDirectory() isn't
formatted correctly. I'm posting from a Web-based client. My employer
blocks NNTP).
folder to a different location, delete the contents of the original
folder, and fill the original location with new files. It's a relatively
simple process, but I've run into a little snag. With Full Control
privileges, I am getting an error stating that I do not have permission
to delete a file in the original location.
I do not believe that this file is in use, because I can successfully
use Directory.Move() without a problem. So I'm beginning to wonder if
there's a problem with my CopyDirectory() method:
public void CopyDirectory(string source, string destination)
{
if (destination[destination.Length - 1] !=
Path.DirectorySeparatorChar)
destination += Path.DirectorySeparatorChar;
if (!Directory.Exists(destination))
Directory.CreateDirectory(destination);
string[] files = Directory.GetFileSystemEntries(source);
foreach (string fileElement in files)
{
// Subdirectories
if (Directory.Exists(fileElement))
CopyDirectory(fileElement,
destination + Path.GetFileName(fileElement));
// Files in current directory
else
File.Copy(fileElement,
destination + Path.GetFileName(fileElement), true);
}
}
Since Directory.Move() works, I know that I can move the original,
create a new folder of the same name in the original's original
location, and populate the new folder. But since this was my first
thought, I want to find out why I'm getting a permissions error.
Anyone have any thoughts? (I apologize if CopyDirectory() isn't
formatted correctly. I'm posting from a Web-based client. My employer
blocks NNTP).