471,627 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

Do not have permission...

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

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Jan 13 '06 #1
2 1442
Jeremy wrote:
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).

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com


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
Jan 13 '06 #2
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!

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Jan 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Russ | last post: by
2 posts views Thread by Taishi | last post: by
7 posts views Thread by Mike L. | last post: by
6 posts views Thread by Steve Richter | last post: by
3 posts views Thread by Marius Groenendijk | last post: by
4 posts views Thread by xzzy | last post: by
1 post views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.