Lock a file?
-
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 ;)
-
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 ;)
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
-
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 ;)
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. ;) -
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. ;)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. -
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. ;)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