Open File Readonly

Open file ReadOnly

The typical problem is that the other process has the file open for writing. All of the standard File methods and StreamReader constructors open the file with FileShare.Read. That cannot work, that denies write sharing. You cannot deny writing, the other process was first and got write access. So you'll be denied access instead.

You have to use FileShare.ReadWrite, like this:

var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
using (var sr = new StreamReader(fs))
{
// etc...
}

Beware that you'll still have a tricky problem, you are reading a half-written file. The other process flushes data to the file at random points in time, you may well read only half a line of text. YMMV.

Open file read-only

You need to set the file attributes of the file before you start the process and then set them back when you opened it.

Example:

var attributes = File.GetAttributes(path);

File.SetAttributes(filePath, attributes | FileAttributes.ReadOnly);

System.IO.Diagnostics.Process.Start(fileName);

File.SetAttributes(filePath, attributes);

Note: This will change the file attributes of the actual file, so keep that in mind.

How to open a file as read-only with java?

There is the method File#setWritable(boolean) you could use:

// get a file
File file = shl.getCurrentTab().getFileDemo();

// disallow write operations
file.setWritable(false);

Re-open file read-only in Linux (or POSIX)

This is not possible at least through call to fcntl as the POSIX docs says (emphasis is mine):

fcntl():

F_SETFL

Set the file status flags, defined in fcntl.h, for the file
description associated with fildes from the corresponding bits in the
third argument, arg, taken as type int. Bits corresponding to the file
access mode and the file creation flags, as defined in fcntl.h, that
are set in arg shall be ignored
. If any bits in arg other than those
mentioned here are changed by the application, the result is
unspecified.

and

fcntl.h

O_ACCMODE Mask for file access modes.

The header shall define the
following symbolic constants for use as the file access modes for
open(), openat(), and fcntl(). The values shall be unique, except that
O_EXEC and O_SEARCH may have equal values. The values shall be
suitable for use in #if preprocessing directives.

O_EXEC Open for execute only (non-directory files). The result is
unspecified if this flag is applied to a directory.

O_RDONLY Open for
reading only.

O_RDWR Open for reading and writing.

O_SEARCH Open
directory for search only. The result is unspecified if this flag is
applied to a non-directory file.

O_WRONLY Open for writing only.

Open a file in a tab in vim in readonly mode

To open a file in read only mode in a new tab, use

tab sview /path/to/file

To open the file in the same pane, (without using a new window or tab), use

view /path/to/file

Note that tab view /path/to/file does not open a new tab.

C++ open a file as read-only

The class ifstream is for reading only so, problem solved. Also, did you really mean to check argc after using argv[1] ?

On the other hand, when you use fstream you need to specify how you want to open the file:

fstream f;
f.open("file", fstream::in | fstream::out); /* Read-write. */


Related Topics



Leave a reply



Submit