Do not have permission...

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).
 
C

Chris

Jeremy said:
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).

Just a thought, but you are trying to Delete the file after the Copy
correct? Is it the case that the file isn't done copying yet when you
try to delete it, therefore making it "in use"? Like I said, just a
thought.


Chris
 
J

Jeremy

Howdy, Chris.

I don't think that would be the problem. Both operations are ran in the
same thread, and the deletion comes after the copy. But it is worth
checking into. Thanks for the thought, and hopefully that will be the
problem =)

Thanks a bunch!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top