IO.File.Delete gives me Unauthorized Access Exception

G

Guest

Just to be clear, I've pasted the message below. I'm doing:
IF IO.File.Exists(filePath) then io.File.Delete(filePath)
It pukes. I know it's security. What I'm trying to do is download a file. I want it to overright the file. (Version checking is done already.) I'm sure it's something I'm missing. What can I check?


Here's the message, just to be clear.
System.UnauthorizedAccessException: Access to the path "C:\Documents and Settings\mark.CENTURYCOLOR\My Documents\Visual Studio Projects\Data.Match\bin\DataMatch.exe" is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Delete(String path)
at DataMatchUpdater.DmUpdater.WriteFiles() in C:\Documents and Settings\mark.CENTURYCOLOR\My Documents\Visual Studio Projects\DataMatchUpdater\DMUpdater.vb:line 187
 
G

Guest

I would assume, since I gave Everyone rights... hmm... maybe I need to apply rights to the folder.
.... Nope, added Everyone to security for this folder, and still with the error.

Also this isn't an ASP app.
 
L

Lebrun Thomas

Also this isn't an ASP app

Arf, forgive me : i've misread your previous message.....
So i don't understand why there is a problem, if application is run under
admin account and if everyone have permissions....
 
R

Rhett Gong [MSFT]

Hi,
From your description, this file is a local file (not readonly) and you have all
permissions to it. And you get this exception when you are trying to delete a file
which you are downloading at the same time.

From your exception msg, we can see that the msg is throwed by WriteFiles. From call
stack, I assume that you write this function and you are trying to write something to
the file. Could you add an assert before the WriteFiles to see if the file exists? If
this file is created after the original file is deleted. I suggest you checking the
permission of the newly created file before going to WriteFiles.

Please let me know what you get after apply this. Thanks.


Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
R

Rhett Gong [MSFT]

Please apply following steps and let me know if it could resolve your problem (Note:
this is just a simple workaround for this problem, for more information, please check
documents about Code Access Security, thanks).

Open the .net configuration tool (under Control Panel) and set write permission for the
specific code group. eg. you can simply set the user /machine Code Groups -> All_Code's
permission set to FullTrust to grant the code all permission to delete the file.


Thanks,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
G

Guest

Earlier in my code I was doing an Assembly.LoadFile on the file, thinking that if I then set my assembly object to nothing, the file would unload. Not so. So far I have found no way to unload the assembly (which is why access was denied).

As a work around, and this solved my problem, I open the file with a fileStream, do a binary reader into a Byte array, and then get the version out of that doing an Assembly.Load on the Byte array.

This way I get the version I need for Version checking, without locking the file.

Thank you. This has taught me a bit about Assemblies.
 
R

Rhett Gong [MSFT]

Thanks for your feedback. It is my pleasure to be working with you. :)

Best regards,
Rhett Gong [MSFT]
Microsoft Online Partner Support

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 

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