Jump to content
Xtreme .Net Talk

Cannot system.io.file.delete after system.io.file.copy() - permission error


Recommended Posts

Posted

Here's my code

 

System.IO.File.Copy("C:\Program Files\My Program\Temp\temp.txt", "C:\temp2.txt")

System.IO.File.Delete("C:\Program Files\My Program\Temp\temp2.txt")

 

I get an exception System.UnauthorizedAccessException "Access to the patch "C:\Program Files\My Program\Temp\temp2.txt" is denied."

 

Are there other classes or methods I could use to workaround this.

  • *Experts*
Posted
Was that file open in some other program (or perhaps by a StreamWriter/Reader in your own program), and are you sure the file exists?
Posted

Two things I should have mentioned...

 

1. This code is in a web application

2. When I manually view the security properties of temp.txt "everyone" has full control. However, when I try to view the security properties of temp2.txt I get the following dialog box "You do not have permission to view or edit the current permission settings for temp2.txt, but you can take ownership or change auditing settings."

 

VolteFace, I am sure no other program is using "temp2.txt" and this is the only place in my code that references "temp2.txt".

Posted

After adding ASPNET to the administrators group I am able to delete the copied file. However, this is not going to be a valid option because this could create a security hole, "big enough for me to drive my hummer thru".

 

Does anyone know of a way for the file that is copied to simply inherit the original file's permissions?

Posted

Normally when you copy a file from an NTFS partition to the same NTFS partition the permissions are duplicated. However, your code is not trying to delete the same file that it is copying. Maybe this is intended or just a typo, but the two lines of code seem to be unrelated.

 

System.IO.File.Copy("C:\Program Files\My Program\Temp\temp.txt", "C:\temp2.txt")

 

^This code creates "C:\temp2.txt" which should have the same permissions as the original file.

 

System.IO.File.Delete("C:\Program Files\My Program\Temp\temp2.txt")

 

^This code tries to delete a different file than the one mentioned above. Therefore "C:\Program Files\My Program\Temp\temp2.txt" may not have the same permissions as the original file you did the copy on.

 

Make sense?

Posted

You could also give the ASP.NET user delete access to the directory where the file is being written in case the copied file is getting its permissions from the directory above it (so they would have read, write, and delete access to files in that directory).

 

Just don't give execute access and write access to the same files.

Posted
'Everyone' has full control of the folder directory and the original file. I don't know why neither one of those permissions are inherited...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...