Open File in Exclusive Mode in C#

How to open a file for non-exclusive write access using .NET

Use the FileShare enumeration when opening the file using File.Open. Specifically, use FileShare.ReadWrite.

I need to know a method to determine if a file is open in exclusive mode or otherwise (read-write etc.)?

You could use CreateFile function to test file for accessing from your thread.

What Docs said:

Creates or opens a file or I/O device. <..> The function returns a handle that can be used to access the file or device for various types of I/O depending on the file or device and the flags and attributes specified.

Let's write some code:

procedure Button1Click(Sender: TObject);
var
FileName: String;
FileHandle: THandle;
Flags: Cardinal;
LastError: Cardinal;
TextErrorCode: PChar;

procedure DisplayNotification;
begin
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER or
FORMAT_MESSAGE_FROM_SYSTEM,
nil, LastError, LANG_USER_DEFAULT,
@TextErrorCode, 0, nil
);
ShowMessage(TextErrorCode);
LocalFree(HLOCAL(TextErrorCode));
end;

begin
FileName := 'YourPath + YourFileName';
Flags := GetFileAttributes(PChar(FileName));
if Flags <> INVALID_FILE_ATTRIBUTES then
begin
if (faDirectory and Flags) <> faDirectory then
begin
FileHandle := CreateFile(PChar(FileName),
GENERIC_READ, FILE_SHARE_READ,
nil, OPEN_EXISTING, 0, 0
);
LastError := GetLastError;
try
if FileHandle <> INVALID_HANDLE_VALUE then
begin
// Execute you backup code
end;
finally
CloseHandle(FileHandle);
end;

// Notify user about problems with opening the file
if FileHandle = INVALID_HANDLE_VALUE then
DisplayNotification;
end
else
// Notification about specified filename defines a directory not a single file
end
else
begin
// Notify user if there is a problem with getting file's attributes
LastError := GetLastError;
DisplayNotification;
end;
end;

Now you can check if file is taken by another process and if it is not then do your code to backup opened file.

Useful links:

  1. GetFileAttributes function
  2. FormatMessage function

Read-only file in use right after read-only set to false

Message: The process cannot access the file '\filepath\filename.pdf' because it is being used by another process.

This is not the same as the file being read-only.

You can find out in code which process is locking the file

https://stackoverflow.com/a/20623311/141172

You can also find out from the command line

UPDATE

Based on your comments, it seems like you may want an exclusive lock on the file for the duration that you are processing it

open file in exclusive mode in C#

Command-line tool for finding out who is locking a file

fopen for writing but not exclusive

Shared access on files is an OS-specific feature. fopen is too generic and does not provide that kind of control. You would need to use something more specific. If you are using a microsoft platform (assuming since you tagged for VS 2015) you can use _fsopen or _wfsopen - they have a third parameter to specify the shared access.

_fsopen("file", "w", _SH_DENYWR);

this will open the file for writing and allow others to read from it (but not write to it).

The argument shflag is a constant expression consisting of one of
the following manifest constants, defined in Share.h.

  • Term Definition
  • _SH_COMPAT Sets Compatibility mode for 16-bit applications.
  • _SH_DENYNO Permits read and write access.
  • _SH_DENYRD Denies read access to the file.
  • _SH_DENYRW Denies read and write access to the file.
  • _SH_DENYWR Denies write access to the file.

Other OS/platforms may support some variation of fsopen too.

Open image file with exclusivity

I think what you need is ...

http://msdn.microsoft.com/en-us/library/5h0z48dh.aspx

http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx

Another way that you could do this is to have a lock file for the image if another App has it open. The lock file can just be a dummy txt file and if it exists then you can assume the file is in use by another process.



Related Topics



Leave a reply



Submit