none
Access to the path is denied when doing File.Delete()

    Question

  • Hi,

    In fact, I have searched through this forum for similar post but it seems no post can solve my case.

    I have a C# program to access a network file share (Machine A) from another computer (Machine B). Both computers join a windows AD domain and an account is logged on in Machine B and the account is granted to have modify write and enough share permission to the file share. I confirmed this with windows explorer to do a file delete on the file share.

    However, when the C# program tries to delete files on the file share, the program returns "Access to path ..... is denied". I really have no idea for this error. I do this on development and UAT and found no problem at all.

    One point really makes me confused is that the C# program can copy a file from the network share to the another network file share on the same computer. I mean on the Machine A, there are two file network shares and I copy a file from one share (P) to another file share (Q) and try to delete the file from share (P). Of course, the delete operation is failed.

    Is there any permission setting need to set this? Microsoft .NET Framework permission need to set??

    Please help if you know any solution or hints to this problem.

    Many thanks,

    Raymond

    Wednesday, May 12, 2010 4:19 PM

Answers

  • Sorry Sorry, I finally found it is my mistake.

     

    I found that although the network share folder has permission to modify file for an account, but

    all files under this folder do not contain the modify permission. After I add the permission back,

    the problem is fixed.

     

    Sorry for any convenience caused.

     

    Thanks,

    Thursday, May 13, 2010 4:56 PM

All replies

  • I just read some articles in Internet, is it related to CAS settings? 

    But I have not set any permissions in .NET Framework 2.0 and the program can still

    read file from the file share, access database connection and send email.

    What can I do?? Please help.

    Wednesday, May 12, 2010 4:47 PM
  • Is the exception that is being thrown an UnauthorizedAccessException? If it is, then this is not related to CAS settings.

    At a guess, this is probably related to UAC - can you try running your application as an Administrator (right-click and choose Run as administrator) and see if that enables you to delete the file? If so, then Explorer (being a built-in Windows application) is allowing you to elevate the current user to an Admin enabling you to delete the file.


    Base Class Library Team (BCL) | My Blog: http://davesbox.com
    Thursday, May 13, 2010 1:49 AM
    Moderator
  • Sorry Sorry, I finally found it is my mistake.

     

    I found that although the network share folder has permission to modify file for an account, but

    all files under this folder do not contain the modify permission. After I add the permission back,

    the problem is fixed.

     

    Sorry for any convenience caused.

     

    Thanks,

    Thursday, May 13, 2010 4:56 PM
  • No problems - glad to hear that you solved your problem.
    Base Class Library Team (BCL) | My Blog: http://davesbox.com
    Thursday, May 13, 2010 5:03 PM
    Moderator