La mejor manera de hacer un archivo escribible en c#


Estoy tratando de establecer una bandera que hace que la casilla de verificación Read Only aparezca cuando right click \ Properties en un archivo.

Gracias!

Author: Alex Jolig, 2009-07-29

3 answers

De dos maneras:

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
fileInfo.IsReadOnly = true/false;

O

// Careful! This will clear other file flags e.g. FileAttributes.Hidden
File.SetAttributes(filePath, FileAttributes.ReadOnly/FileAttributes.Normal);

La propiedad IsReadOnly en FileInfo esencialmente hace el cambio de bits que tendría que hacer manualmente en el segundo método.

 61
Author: Rex M,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-06-12 07:35:45

A establecer la bandera de solo lectura, en efecto haciendo que el archivo no sea escribible:

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

To remove the read-only flag, in effect making the file writeable:

File.SetAttributes(filePath,
    File.GetAttributes(filePath) & ~FileAttributes.ReadOnly);

Para cambiar la bandera de solo lectura, por lo que es lo contrario de lo que es en este momento:

File.SetAttributes(filePath,
    File.GetAttributes(filePath) ^ FileAttributes.ReadOnly);

Esto es básicamente máscaras de bits en efecto. Se establece un bit específico para establecer la bandera de solo lectura, se borra para eliminar la bandera.

Tenga en cuenta que el código anterior no cambiará ningún otro propiedades del archivo. En otras palabras, si el archivo estaba oculto antes de ejecutar el código anterior, también permanecerá oculto después. Si simplemente establece los atributos del archivo en .Normal o .ReadOnly, podría terminar perdiendo otras banderas en el proceso.

 34
Author: Lasse Vågsæther Karlsen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-29 18:23:10

C#:

Archivo.setAttributes (filePath, FileAttributes.Normal);

Archivo.setAttributes (filePath, FileAttributes.ReadOnly);

 0
Author: ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-07-29 18:09:10