Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. lock a file

lock a file

Scheduled Pinned Locked Moved C#
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    swjam
    wrote on last edited by
    #1

    is it possible to lock a file so that only a certain application has privelege to write to it? how? i want to create an application that makes use of a log file such that the log file cannot be modified outside the application. Also, this means that only those users that have access to run my application will be able to modify the log file (through the application). thanks in advance.

    I am a SysAdmin, I battle my own daemons.

    C M 2 Replies Last reply
    0
    • S swjam

      is it possible to lock a file so that only a certain application has privelege to write to it? how? i want to create an application that makes use of a log file such that the log file cannot be modified outside the application. Also, this means that only those users that have access to run my application will be able to modify the log file (through the application). thanks in advance.

      I am a SysAdmin, I battle my own daemons.

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Have the application open the file and set the sharing appropriately: See: * File.Open[^] * FileShare[^]


      Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

      C 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Have the application open the file and set the sharing appropriately: See: * File.Open[^] * FileShare[^]


        Upcoming events: * Edinburgh: Web Security Conference Day for Windows Developers (12th April) * Glasgow: Introduction to AJAX (2nd May), SQL Server, Mock Objects My website

        C Offline
        C Offline
        coolestCoder
        wrote on last edited by
        #3

        Hi Colin Angus Mackay, I think DenClancy wants that a file ( even if a simple text file ) must not be opened without a particular application. Is this possible ? What if an Admin user tries to open this file ? Actually I wanted to restrict access to some files, even if the files are simple text files (even to admin users). I heard about something called Access Control List. Is it applicable here ? I dont know the details of ACL. If possible, please reply in details. Thanks.


        "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


        coolestCoder

        P 1 Reply Last reply
        0
        • C coolestCoder

          Hi Colin Angus Mackay, I think DenClancy wants that a file ( even if a simple text file ) must not be opened without a particular application. Is this possible ? What if an Admin user tries to open this file ? Actually I wanted to restrict access to some files, even if the files are simple text files (even to admin users). I heard about something called Access Control List. Is it applicable here ? I dont know the details of ACL. If possible, please reply in details. Thanks.


          "A good programmer is someone who looks both ways before crossing a one-way street." -- Doug Linder


          coolestCoder

          P Offline
          P Offline
          PlayByTheRules
          wrote on last edited by
          #4

          coolestCoder wrote:

          Is this possible ?

          As I pointed out, the application can deny others the ability to do anything but read the file. Or it can take exclusive control over the file too. See the previous links to FileShare.

          coolestCoder wrote:

          What if an Admin user tries to open this file ?

          Yes, even an administrator - Try it with a SQL Server database file. You won't be able modify it regardless of who you are logged in as because the SQL Server process has control over it. Not even someone logged on as the same user as SQL Server can access the file.

          coolestCoder wrote:

          ctually I wanted to restrict access to some files, even if the files are simple text files (even to admin users).

          You can change the permissions on a file as you need it. I'm not sure if you can lock an admin out though. If it is possible then it would only be a temporary situation as the admin would have the ability to grant themselves access again.

          M 1 Reply Last reply
          0
          • P PlayByTheRules

            coolestCoder wrote:

            Is this possible ?

            As I pointed out, the application can deny others the ability to do anything but read the file. Or it can take exclusive control over the file too. See the previous links to FileShare.

            coolestCoder wrote:

            What if an Admin user tries to open this file ?

            Yes, even an administrator - Try it with a SQL Server database file. You won't be able modify it regardless of who you are logged in as because the SQL Server process has control over it. Not even someone logged on as the same user as SQL Server can access the file.

            coolestCoder wrote:

            ctually I wanted to restrict access to some files, even if the files are simple text files (even to admin users).

            You can change the permissions on a file as you need it. I'm not sure if you can lock an admin out though. If it is possible then it would only be a temporary situation as the admin would have the ability to grant themselves access again.

            M Offline
            M Offline
            mav northwind
            wrote on last edited by
            #5

            Hi! I think the real question is how to permanently lock a file without an application that has to keep the file handle open all the time. Sure you can set FileShare to None, but the moment your application ends anyone can access the file again. The original poster wanted a way to deny access to a file for everybody even after the application has finished.

            Regards, mav -- Black holes are the places where God divided by 0...

            1 Reply Last reply
            0
            • S swjam

              is it possible to lock a file so that only a certain application has privelege to write to it? how? i want to create an application that makes use of a log file such that the log file cannot be modified outside the application. Also, this means that only those users that have access to run my application will be able to modify the log file (through the application). thanks in advance.

              I am a SysAdmin, I battle my own daemons.

              M Offline
              M Offline
              mav northwind
              wrote on last edited by
              #6

              I think you could achieve something similar if you can create a separate user with a random password only your application knows and have your application set the access rights for the file so that only this particular user has access rights. But an administrator could take ownership of the file and thus modify the access rights anyway...

              Regards, mav -- Black holes are the places where God divided by 0...

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups