File.SetAttributes(String, FileAttributes) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Legt die angegebenen FileAttributes der Datei im angegebenen Pfad fest.
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)
Parameter
- path
- String
Der Pfad zur Datei.
- fileAttributes
- FileAttributes
Eine bitweise Kombination der Enumerationswerte.
Ausnahmen
.NET Framework und .NET Core-Versionen vor 2.1: ist leer, enthält nur Leerzeichen, enthält ungültige Zeichen, oder das Dateiattribut path
ist ungültig.
Der angegebene Pfad und/oder Dateiname überschreiten die vom System definierte maximale Länge.
path
weist ein ungültiges Format auf.
Der angegebene Pfad ist ungültig (er befindet sich z. B. auf einem nicht zugeordneten Laufwerk).
Die Datei kann nicht gefunden werden.
path
hat eine schreibgeschützte Datei angegeben.
- oder - Dieser Vorgang wird von der aktuellen Plattform nicht unterstützt.
- oder -
path
hat ein Verzeichnis angegeben.
- oder - Der Aufrufer verfügt nicht über die erforderliche Berechtigung.
Beispiele
Im folgenden Beispiel werden die Methoden und veranschaulicht, indem die Attribute GetAttributes
und auf eine Datei SetAttributes
Archive
Hidden
anwenden.
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
Hinweise
Der path
-Parameter kann relative oder absolute Pfadinformationen angeben. Relative Pfadinformationen werden als relativ zum aktuellen Arbeitsverzeichnis interpretiert. Informationen zum Abrufen des aktuellen Arbeitsverzeichnisses finden Sie unter GetCurrentDirectory .
Bestimmte Dateiattribute, z. Hidden B. und ReadOnly , können kombiniert werden. Andere Attribute, z. B. Normal , müssen allein verwendet werden.
Es ist nicht möglich, den Komprimierungsstatus eines File -Objekts mithilfe der -Methode zu SetAttributes ändern.
Eine Liste der allgemeinen E/A-Aufgaben finden Sie unter Allgemeine E/A-Aufgaben.