英語で読む

次の方法で共有


File.SetAttributes メソッド

定義

オーバーロード

SetAttributes(SafeFileHandle, FileAttributes)

に関連付けられているfileHandleファイルまたはディレクトリの指定した FileAttributes を設定します。

SetAttributes(String, FileAttributes)

指定したパスでファイルの指定された FileAttributes を設定します。

SetAttributes(SafeFileHandle, FileAttributes)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

に関連付けられているfileHandleファイルまたはディレクトリの指定した FileAttributes を設定します。

public static void SetAttributes (Microsoft.Win32.SafeHandles.SafeFileHandle fileHandle, System.IO.FileAttributes fileAttributes);

パラメーター

fileHandle
SafeFileHandle

SafeFileHandle設定する必要があるfileAttributesファイルまたはディレクトリの 。

fileAttributes
FileAttributes

列挙値のビットごとの組み合わせ。

例外

fileHandlenullです。

呼び出し元に、必要なアクセス許可がありません。

注釈

メソッドを使用してSetAttributes(SafeFileHandle, FileAttributes)オブジェクトの圧縮状態をFile変更することはできません。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 7, 8, 9

SetAttributes(String, FileAttributes)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

指定したパスでファイルの指定された FileAttributes を設定します。

public static void SetAttributes (string path, System.IO.FileAttributes fileAttributes);

パラメーター

path
String

ファイルへのパス。

fileAttributes
FileAttributes

列挙値のビットごとの組み合わせ。

例外

2.1 より前のバージョンの.NET Frameworkと .NET Core: path が空であるか、空白だけが含まれているか、無効な文字が含まれているか、file 属性が無効です。

指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。

path の形式が正しくありません。

指定されたパスが無効です (たとえば、マップされていないドライブにあるなど)。

ファイルが見つかりません。

path が読み取り専用のファイルを指定しました。

- または -

この操作は、現在のプラットフォームではサポートされていません。

- または -

path がディレクトリを指定しました。

- または -

呼び出し元に、必要なアクセス許可がありません。

次の例では、 GetAttributes および 属性をArchiveファイルに適用する メソッドと Hidden メソッドSetAttributesを示します。

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;
    }
}

注釈

パラメーターは path 、相対パスまたは絶対パス情報を指定できます。 相対パス情報は、現在の作業ディレクトリに対する相対パスとして解釈されます。 現在の作業ディレクトリを取得するには、「」を参照してください GetCurrentDirectory

ReadOnlyなどのHidden特定のファイル属性を組み合わせることができます。 などの Normal他の属性は、単独で使用する必要があります。

メソッドを使用してSetAttributesオブジェクトの圧縮状態をFile変更することはできません。

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0