Lock file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • macneed

    Lock file

    Can i lock a file in c#,
    that i can move it and del it,
    but other program don't have right to access(read) until i release it?

    i read fileshare.none, but can't delete it without close the filestream or
    streamwriter
    there was still a cocurrency problem if i close the stream and delete it
    just after close
    what can i do?


  • Peter Duniho

    #2
    Re: Lock file

    On Tue, 26 Aug 2008 03:21:20 -0700, macneed <macneed@yahoo. com.hkwrote:
    Can i lock a file in c#,
    that i can move it and del it,
    but other program don't have right to access(read) until i release it?
    >
    i read fileshare.none, but can't delete it without close the filestream
    or
    streamwriter
    there was still a cocurrency problem if i close the stream and delete it
    just after close
    what can i do?
    As far as I know, nothing. The file API just doesn't work that way. You
    can assert exclusive access only while you have the file open, and you can
    move or delete the file only while the file _isn't_ open.

    If you want your application to have exclusive move and delete rights for
    the file, you need to do that at the file security attributes level. Your
    application would have to run as a user that is exclusively granted those
    rights in the file's access control list.

    Pete

    Comment

    • Pavel Minaev

      #3
      Re: Lock file

      On Aug 26, 2:21 pm, "macneed" <macn...@yahoo. com.hkwrote:
      Can i lock a file in c#,
      that i can move it and del it,
      but other program don't have right to access(read) until i release it?
      >
      i read fileshare.none, but can't delete it without close the filestream or
      streamwriter
      there was still a cocurrency problem if i close the stream and delete it
      just after close
      what can i do?
      I don't see why you'd want to forbid other processes from deleting
      your file while you're working with it. Deletion will not delete the
      actual data for as long as the file remains open - so, if another
      process requests to delete the file, it will become invisible, but
      your program can finish doing whatever it was doing with it. When you
      close your file handle, the file will be physically deleted.

      Comment

      Working...