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#
csharpxmlquestion
5 Posts 4 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.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    How can you lock a file in .NET? I have a windows application that reads xml files. If two users are trying to read the same file, I want to lock the file so the second user can't read or change the file. I'm looking for the most bare bones lock and unlock functionality. Thanks ;)

    H M 2 Replies Last reply
    0
    • A Anonymous

      How can you lock a file in .NET? I have a windows application that reads xml files. If two users are trying to read the same file, I want to lock the file so the second user can't read or change the file. I'm looking for the most bare bones lock and unlock functionality. Thanks ;)

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      The System.IO.FileStream can do this. Read the .NET Framework SDK for more information:

      FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None);
      XmlTextReader reader = new XmlTextReader(stream);
      try
      {
      // Read from "reader" or something.
      }
      finally
      {
      reader.Close();
      stream.Close();
      }

      This is a very simplistic example. Read about the FileStream constructor for more information.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      • A Anonymous

        How can you lock a file in .NET? I have a windows application that reads xml files. If two users are trying to read the same file, I want to lock the file so the second user can't read or change the file. I'm looking for the most bare bones lock and unlock functionality. Thanks ;)

        M Offline
        M Offline
        Manster
        wrote on last edited by
        #3

        I just figured this out: private bool LockFile( string sFilePathToLock ) { bool bFileLocked = false; if( File.Exists( sFilePathToLock ) && ( File.GetAttributes( sFilePathToLock ) != FileAttributes.ReadOnly ) ) { File.SetAttributes( sFilePathToLock, FileAttributes.ReadOnly ); // Use the FileAttributes.Archive to mark a file for deletion. bFileLocked = true; } return bFileLocked; } private bool UnLockFile( string sFilePathToUnLock ) { bool bFileUnLocked = false; if( File.Exists( sFilePathToUnLock ) && ( File.GetAttributes( sFilePathToUnLock ) == FileAttributes.ReadOnly ) ) { File.SetAttributes( sFilePathToUnLock, FileAttributes.Normal ); bFileUnLocked = true; } return bFileUnLocked; } Hope this helps. ;)

        L H 2 Replies Last reply
        0
        • M Manster

          I just figured this out: private bool LockFile( string sFilePathToLock ) { bool bFileLocked = false; if( File.Exists( sFilePathToLock ) && ( File.GetAttributes( sFilePathToLock ) != FileAttributes.ReadOnly ) ) { File.SetAttributes( sFilePathToLock, FileAttributes.ReadOnly ); // Use the FileAttributes.Archive to mark a file for deletion. bFileLocked = true; } return bFileLocked; } private bool UnLockFile( string sFilePathToUnLock ) { bool bFileUnLocked = false; if( File.Exists( sFilePathToUnLock ) && ( File.GetAttributes( sFilePathToUnLock ) == FileAttributes.ReadOnly ) ) { File.SetAttributes( sFilePathToUnLock, FileAttributes.Normal ); bFileUnLocked = true; } return bFileUnLocked; } Hope this helps. ;)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Although I'm a C# newbie, I can see Manster's solution will not work. See my (AS) comments below: if( File.Exists( sFilePathToLock ) && ( File.GetAttributes( sFilePathToLock ) != FileAttributes.ReadOnly ) ) { // AS Another thread/process could lock the file here, before this thread gets a chance // to lock it. Therefore the SetAttributes call will fail (throw an exception?). File.SetAttributes( sFilePathToLock, FileAttributes.ReadOnly ); // Use the FileAttributes.Archive to mark a file for deletion. bFileLocked = true; } return bFileLocked; } The original reply (to use FileStream) seems more like it, but you still need to trap the error when it fails to open the file because it is already locked.

          1 Reply Last reply
          0
          • M Manster

            I just figured this out: private bool LockFile( string sFilePathToLock ) { bool bFileLocked = false; if( File.Exists( sFilePathToLock ) && ( File.GetAttributes( sFilePathToLock ) != FileAttributes.ReadOnly ) ) { File.SetAttributes( sFilePathToLock, FileAttributes.ReadOnly ); // Use the FileAttributes.Archive to mark a file for deletion. bFileLocked = true; } return bFileLocked; } private bool UnLockFile( string sFilePathToUnLock ) { bool bFileUnLocked = false; if( File.Exists( sFilePathToUnLock ) && ( File.GetAttributes( sFilePathToUnLock ) == FileAttributes.ReadOnly ) ) { File.SetAttributes( sFilePathToUnLock, FileAttributes.Normal ); bFileUnLocked = true; } return bFileUnLocked; } Hope this helps. ;)

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #5

            This is not a correct solution. This only makes the file read-only to programs that check it. See my post for the correct solution, a solution that has been used for as long as filesystems have been around. This is the way it's supposed to be done and truly locks it while it's open from reading or writing by other processes.

            Microsoft MVP, Visual C# My Articles

            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