File.SetAttributes メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オーバーロード
| 名前 | 説明 |
|---|---|
| SetAttributes(SafeFileHandle, FileAttributes) |
|
| SetAttributes(String, FileAttributes) |
指定したパス上のファイルの指定した FileAttributes を設定します。 |
SetAttributes(SafeFileHandle, FileAttributes)
- ソース:
- File.cs
- ソース:
- File.cs
- ソース:
- File.cs
- ソース:
- File.cs
- ソース:
- File.cs
fileHandleに関連付けられているファイルまたはディレクトリの指定したFileAttributesを設定します。
public:
static void SetAttributes(Microsoft::Win32::SafeHandles::SafeFileHandle ^ fileHandle, System::IO::FileAttributes fileAttributes);
public static void SetAttributes(Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle, System.IO.FileAttributes fileAttributes);
static member SetAttributes : Microsoft.Win32.SafeHandles.SafeFileHandle * System.IO.FileAttributes -> unit
Public Shared Sub SetAttributes (fileHandle As SafeFileHandle, fileAttributes As FileAttributes)
パラメーター
- fileHandle
- SafeFileHandle
fileAttributesを設定する必要があるファイルまたはディレクトリへのSafeFileHandle。
- fileAttributes
- FileAttributes
列挙値のビットごとの組み合わせ。
例外
fileHandle は nullです。
呼び出し元に必要なアクセス許可がありません。
注釈
Fileメソッドを使用して、SetAttributes(SafeFileHandle, FileAttributes) オブジェクトの圧縮状態を変更することはできません。
適用対象
SetAttributes(String, FileAttributes)
- ソース:
- File.cs
- ソース:
- File.cs
- ソース:
- File.cs
- ソース:
- File.cs
- ソース:
- File.cs
指定したパス上のファイルの指定した FileAttributes を設定します。
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)
パラメーター
- path
- String
ファイルへのパス。
- fileAttributes
- FileAttributes
列挙値のビットごとの組み合わせ。
例外
.NET Framework および .NET Core バージョン 2.1 より前のバージョン: path が空で、空白のみが含まれているか、無効な文字が含まれているか、ファイル属性が無効です。
指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。
path が無効な形式です。
指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。
ファイルが見つかりません。
path は、読み取り専用のファイルを指定しました。
-又は-
この操作は、現在のプラットフォームではサポートされていません。
-又は-
path はディレクトリを指定しました。
-又は-
呼び出し元に必要なアクセス許可がありません。
例
次の例では、GetAttributes属性とSetAttributes属性をファイルに適用して、ArchiveメソッドとHiddenメソッドを示します。
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;
}
}
open System.IO
open System.Text
let removeAttribute attributes attributesToRemove = attributes &&& ~~~attributesToRemove
let path = @"c:\temp\MyTest.txt"
// Create the file if it does not exist.
if File.Exists path |> not then
File.Create path |> ignore
let attributes = File.GetAttributes path
if attributes &&& FileAttributes.Hidden = FileAttributes.Hidden then
// Show the file.
let attributes =
removeAttribute attributes FileAttributes.Hidden
File.SetAttributes(path, attributes)
printfn $"The {path} file is no longer hidden."
else
// Hide the file.
File.SetAttributes(path, File.GetAttributes path ||| FileAttributes.Hidden)
printfn $"The {path} file is now hidden."
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
注釈
path パラメーターは、相対パス情報または絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、 GetCurrentDirectoryを参照してください。
HiddenやReadOnlyなど、特定のファイル属性を組み合わせることができます。 Normalなどの他の属性は、単独で使用する必要があります。
Fileメソッドを使用して、SetAttributes オブジェクトの圧縮状態を変更することはできません。
一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。