File.SetAttributes(String, FileAttributes) Méthode
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Définit l'élément FileAttributes spécifié du fichier sur le chemin d'accès spécifié.
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)
Paramètres
- path
- String
Chemin d'accès au fichier.
- fileAttributes
- FileAttributes
Combinaison d'opérations de bits des valeurs d'énumération.
Exceptions
.NET Framework et versions de .net Core antérieures à 2,1 : path
est vide, contient uniquement des espaces blancs, contient des caractères non valides ou l’attribut de fichier n’est pas valide.
Le chemin et/ou le nom de fichier spécifiés dépassent la longueur maximale définie par le système.
path
est dans un format non valide.
Le chemin spécifié n’est pas valide (par exemple, il est sur un lecteur non mappé).
Fichier introuvable.
path
a spécifié un fichier en lecture seule.
ou - Cette opération n'est pas prise en charge sur la plateforme actuelle.
ou -
path
a spécifié un répertoire.ou - L'appelant n'a pas l'autorisation requise.
Exemples
L’exemple suivant illustre les GetAttributes
SetAttributes
méthodes et en appliquant Archive
les Hidden
attributs et à un fichier.
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
Remarques
Le path
paramètre est autorisé à spécifier des informations relatives au chemin d’accès relatif ou absolu. Les informations relatives au chemin d’accès relatif sont interprétées par rapport au répertoire de travail actuel. Pour obtenir le répertoire de travail actuel, consultez GetCurrentDirectory .
Certains attributs de fichier, tels que Hidden et ReadOnly , peuvent être combinés. D’autres attributs, tels que Normal , doivent être utilisés seuls.
Il n’est pas possible de modifier l’état de compression d’un File objet à l’aide de la SetAttributes méthode.
Pour obtenir la liste des tâches d’e/s courantes, consultez tâches d’e/s courantes.