Assembly : mscorlib (dans mscorlib.dll)
SyntaxePublic Shared Sub SetAttributes ( _ path As String, _ fileAttributes As FileAttributes _ )
Dim path As String Dim fileAttributes As FileAttributes File.SetAttributes(path, fileAttributes)
public static void SetAttributes ( string path, FileAttributes fileAttributes )
public: static void SetAttributes ( String^ path, FileAttributes fileAttributes )
public static void SetAttributes ( String path, FileAttributes fileAttributes )
public static function SetAttributes ( path : String, fileAttributes : FileAttributes )
Paramètres
- path
Chemin d'accès au fichier.
- fileAttributes
FileAttributes souhaité, tel que Hidden, ReadOnly, Normal et Archive.
Exceptions| Type d'exception | Condition |
|---|---|
| path est vide, ne contient que des espaces blancs, contient des caractères non valides ou l'attribut de fichier est non valide. | |
| Le chemin d'accès ou le nom de fichier spécifié ou les deux excèdent la longueur maximale définie par le système. Par exemple, sur les plates-formes Windows, les chemins d'accès et les noms de fichiers ne doivent pas dépasser, respectivement, 248 et 260 caractères. | |
| Le format de path est non valide. | |
| Le chemin d'accès spécifié n'est pas valide (il se trouve, par exemple, sur un lecteur non mappé). | |
| Le fichier est introuvable. | |
| path a spécifié un fichier qui est en lecture seule. - ou - Cette opération n'est pas prise en charge sur la plate-forme actuelle. - ou - path a spécifié un répertoire. - ou - L'appelant n'a pas l'autorisation requise. |
NotesLe paramètre path est autorisé à spécifier des informations sur le chemin d'accès relatif ou absolu. Les informations sur le chemin d'accès relatif sont interprétées comme étant relatives au répertoire de travail en cours. Pour obtenir le répertoire de travail en cours, consultez GetCurrentDirectory.
Il n'est pas possible de modifier l'état de compression d'un objet File à l'aide de la méthode SetAttributes.
Pour obtenir un exemple d'utilisation de cette méthode, consultez la section Exemple. Le tableau suivant répertorie des exemples d'autres tâches d'E/S courantes ou apparentées.
| Pour effectuer cette opération... | Consultez l'exemple qui se trouve dans cette rubrique... |
|---|---|
| Créer un fichier texte. | |
| Écrire dans un fichier texte. | |
| Lire à partir d'un fichier texte. | |
| Ajouter du texte dans un fichier. | Comment : ouvrir un fichier journal et y ajouter des éléments |
| Renommer ou déplacer un fichier. | |
| Obtenir les attributs d'un fichier. | |
| Lire à partir d'un fichier binaire. | Comment : lire et écrire dans un fichier de données créé récemment |
| Écrire dans un fichier binaire. | Comment : lire et écrire dans un fichier de données créé récemment |
ExempleL'exemple suivant illustre les méthodes GetAttributes et SetAttributes en appliquant les attributs Archive et Hidden à un fichier.
Imports System 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 If (File.GetAttributes(path) And FileAttributes.Hidden) = FileAttributes.Hidden Then ' Show the file. File.SetAttributes(path, FileAttributes.Archive) 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 End Class
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); } if ((File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden) { // Show the file. File.SetAttributes(path, FileAttributes.Archive); 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); } } }
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, FileAttributes::Archive ); 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 ); } }
import System.*; import System.IO.*; import System.Text.*; class Test { public static void main(String[] args) { 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). Equals(FileAttributes.Hidden)) { // Show the file. File.SetAttributes(path, FileAttributes.Archive); 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); } } //main } //Test
Sécurité .NET Framework- FileIOPermission pour la lecture et l'écriture de fichiers. Énumération associée : FileIOPermissionAccess.Write
Plates-formesWindows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Édition Media Center, Windows XP Professionnel Édition x64, Windows XP SP2, Windows XP Starter Edition
Le .NET Framework ne prend pas en charge toutes les versions de chaque plate-forme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise.
Outils (masquer)
S'enregistrer
Liste des Membres
Qui est en ligne?
FAQ