File.SetAttributes(String, FileAttributes) Método

Definición

Establece el FileAttributes especificado del archivo en la ruta de acceso especificada.

public:
 static void SetAttributes(System::String ^ path, System::IO::FileAttributes fileAttributes);
public static void SetAttributes (string path, System.IO.FileAttributes fileAttributes);
static member SetAttributes : string * System.IO.FileAttributes -> unit
Public Shared Sub SetAttributes (path As String, fileAttributes As FileAttributes)

Parámetros

path
String

Ruta de acceso al archivo.

fileAttributes
FileAttributes

Combinación bit a bit de los valores de la enumeración.

Excepciones

Versiones de .NET Framework y .NET Core anteriores a la 2.1: path están vacías, solo contienen espacios en blanco, contienen caracteres no válidos o el atributo de archivo no es válido.

La ruta de acceso especificada, el nombre de archivo o ambos superan la longitud máxima definida por el sistema.

path está en un formato no válido.

La ruta de acceso especificada no es válida (por ejemplo, está en una unidad no asignada).

No se encuentra el archivo.

path especificó un archivo que es de solo lectura.

o bien Esta operación no es compatible con la plataforma actual.

o bien path especificó un directorio.

o bien El llamador no dispone del permiso requerido.

Ejemplos

En el ejemplo siguiente se muestran los GetAttributes métodos y SetAttributes aplicando los Archive atributos y Hidden a un archivo .

using namespace System;
using namespace System::IO;
using namespace System::Text;
int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   
   // Create the file if it does not exist.
   if (  !File::Exists( path ) )
   {
      File::Create( path );
   }

   if ( (File::GetAttributes( path ) & FileAttributes::Hidden) == FileAttributes::Hidden )
   {
      
      // Show the file.
      File::SetAttributes(path, File::GetAttributes( path ) & ~FileAttributes::Hidden);
      Console::WriteLine( "The {0} file is no longer hidden.", path );
   }
   else
   {
      
      // Hide the file.
      File::SetAttributes( path, static_cast<FileAttributes>(File::GetAttributes( path ) | FileAttributes::Hidden) );
      Console::WriteLine( "The {0} file is now hidden.", path );
   }
}
using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // Create the file if it does not exist.
        if (!File.Exists(path))
        {
            File.Create(path);
        }

        FileAttributes attributes = File.GetAttributes(path);

        if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
        {
            // Show the file.
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden);
            File.SetAttributes(path, attributes);
            Console.WriteLine("The {0} file is no longer hidden.", path);
        }
        else
        {
            // Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now hidden.", path);
        }
    }

    private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)
    {
        return attributes & ~attributesToRemove;
    }
}
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

        ' Create the file if it does not exist.
        If File.Exists(path) = False Then
            File.Create(path)
        End If

        Dim attributes As FileAttributes
        attributes = File.GetAttributes(path)

        If (attributes And FileAttributes.Hidden) = FileAttributes.Hidden Then
            ' Show the file.
            attributes = RemoveAttribute(attributes, FileAttributes.Hidden)
            File.SetAttributes(path, attributes)
            Console.WriteLine("The {0} file is no longer hidden.", path)
        Else
            ' Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) Or FileAttributes.Hidden)
            Console.WriteLine("The {0} file is now hidden.", path)
        End If
    End Sub

    Public Shared Function RemoveAttribute(ByVal attributes As FileAttributes, ByVal attributesToRemove As FileAttributes) As FileAttributes
        Return attributes And (Not attributesToRemove)
    End Function
End Class

Comentarios

El path parámetro puede especificar información de ruta de acceso relativa o absoluta. La información de ruta de acceso relativa se interpreta como relativa al directorio de trabajo actual. Para obtener el directorio de trabajo actual, vea GetCurrentDirectory.

Algunos atributos de archivo, como Hidden y ReadOnly, se pueden combinar. Otros atributos, como Normal, deben usarse solos.

No es posible cambiar el estado de compresión de un File objeto mediante el SetAttributes método .

Para obtener una lista de tareas de E/S comunes, consulte Tareas de E/S comunes.

Se aplica a

Consulte también