FileIOPermissionAttributeとは? わかりやすく解説

FileIOPermissionAttribute クラス

宣言セキュリティを使用して、FileIOPermission のセキュリティ アクションをコードに適用できるようにします。このクラスは継承できません。

名前空間: System.Security.Permissions
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

<SerializableAttribute> _
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Assembly Or AttributeTargets.Class
 Or AttributeTargets.Struct Or AttributeTargets.Constructor
 Or AttributeTargets.Method, AllowMultiple:=True, Inherited:=False)> _
Public NotInheritable Class
 FileIOPermissionAttribute
    Inherits CodeAccessSecurityAttribute
Dim instance As FileIOPermissionAttribute
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method,
 AllowMultiple=true, Inherited=false)] 
public sealed class FileIOPermissionAttribute
 : CodeAccessSecurityAttribute
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[AttributeUsageAttribute(AttributeTargets::Assembly|AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Constructor|AttributeTargets::Method,
 AllowMultiple=true, Inherited=false)] 
public ref class FileIOPermissionAttribute
 sealed : public CodeAccessSecurityAttribute
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method,
 AllowMultiple=true, Inherited=false) */ 
public final class FileIOPermissionAttribute
 extends CodeAccessSecurityAttribute
SerializableAttribute 
ComVisibleAttribute(true) 
AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method,
 AllowMultiple=true, Inherited=false) 
public final class FileIOPermissionAttribute
 extends CodeAccessSecurityAttribute
解説解説

ファイルおよびディレクトリは、絶対パスを使用して指定されます。ファイルにアクセスする場合は、ファイルを作成または開くときに、セキュリティ チェックが実行されます。ファイルを閉じて再度開かない限り、セキュリティ チェックが再び行われることはありません。ファイルに最初にアクセスしたときにアクセス許可を確認することによって、セキュリティ チェックがアプリケーションのパフォーマンスに与える影響を最小限にします。これは、ファイルを開く回数は 1 回だけですが、読み取りと書き込みは複数回行うことができるためです。

宣言の許容スコープは、使用する SecurityAction によって異なります。

セキュリティ属性によって宣言されたセキュリティ情報は、属性ターゲットのメタデータに格納され、実行時にシステムによってアクセスされます。セキュリティ属性は宣言セキュリティにだけ使用されます。強制セキュリティの場合は、対応するアクセス許可クラスを使用します。

注意に関するメモ注意

Unrestricted FileIOPermission は、1 つのファイルにアクセスするための複数のパス名も含め、ファイル システム内のすべてのパスに対するアクセス許可を与えます。ファイルへのアクセスを Deny によって拒否するには、そのファイルへの有効なパスをすべて Deny によって拒否する必要があります。たとえば、\\server\share をネットワーク ドライブ X に割り当てた場合、\\server\share\file へのアクセスを Deny によって拒否するには、そのファイルにアクセスするために使用できる \\server\share\file、X:\file などのパスをすべて Deny によって拒否する必要があります。

使用例使用例

指定したファイルへのフル アクセスのための FileIOPermission を要求する正しい方法と、コードを実行するためには少なくともこのアクセス許可が必要であることを次の宣言属性の例に示します。

 <Assembly: FileIOPermissionAttribute(SecurityAction.RequestMinimum, _
 All := "C:\example\sample.txt")>
'In Visual Basic, you must specify that you are using the assembly scope
 when making a request.
[assembly:FileIOPermissionAttribute(SecurityAction.RequestMinimum, All="C:\\example\\sample.txt")]
//In C#, you must specify that you are using the assembly scope when
 making a request.
[assembly:FileIOPermissionAttribute(SecurityAction::RequestMinimum,All="C:\\example\\sample.txt")];
//In C++, you must specify that you are using the assembly scope when
 making a request.
/** @assembly FileIOPermissionAttribute(SecurityAction.RequestMinimum,
    All = "C:\\example\\sample.txt")
 */
// In VJ#, you must specify that you are using the assembly scope 
// when making a request.

リンク時に、呼び出し元のコードに無制限の FileIOPermission を含めることを要求する方法を次の例に示します。通常、メソッドやクラスを不正の可能性があるコードから保護するための要求をマネージ ライブラリ (DLL) で作成します。

<FileIOPermissionAttribute(SecurityAction.Demand, _
 Unrestricted := True)> Public Class
 SampleClass
[FileIOPermissionAttribute(SecurityAction.Demand, Unrestricted=true)]
[FileIOPermissionAttribute(SecurityAction::Demand,Unrestricted=true)]
/** @attribute FileIOPermissionAttribute(SecurityAction.Demand,
    Unrestricted = true)
 */
継承階層継承階層
System.Object
   System.Attribute
     System.Security.Permissions.SecurityAttribute
       System.Security.Permissions.CodeAccessSecurityAttribute
        System.Security.Permissions.FileIOPermissionAttribute
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
FileIOPermissionAttribute メンバ
System.Security.Permissions 名前空間
FileIOPermission クラス
FileIOPermissionAccess 列挙体
その他の技術情報
属性を使用したメタデータの拡張

FileIOPermissionAttribute コンストラクタ

SecurityAction を指定して、FileIOPermissionAttribute クラスの新しいインスタンスを初期化します。

名前空間: System.Security.Permissions
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub New ( _
    action As SecurityAction _
)
Dim action As SecurityAction

Dim instance As New FileIOPermissionAttribute(action)
public FileIOPermissionAttribute (
    SecurityAction action
)
public:
FileIOPermissionAttribute (
    SecurityAction action
)
public FileIOPermissionAttribute (
    SecurityAction action
)
public function FileIOPermissionAttribute (
    action : SecurityAction
)

パラメータ

action

SecurityAction 値の 1 つ。

例外例外
例外の種類条件

ArgumentException

action パラメータが、有効な SecurityAction ではありません。

使用例使用例
' This sample demonstrates the use of the FileIOPermissionAttribute
 class.
' The sample follows the recommended procedure of first granting PermitOnly
 permissions, 
' then using a Deny on that set of granted permissions.  
Imports System
Imports System.Reflection
Imports System.Security.Permissions
Imports System.Security
Imports System.IO
Imports Microsoft.VisualBasic



Class [MyClass]

    Public Shared Sub FileIOPermissionAttributeDemo()
        Try
            PermitOnlyMethod()
        Catch e As Exception
            Console.WriteLine(e.Message.ToString())
        End Try
        Try
            DenyMethod()
        Catch e As Exception
            Console.WriteLine(e.Message.ToString())
        End Try
        Try
            DenyAllMethod()
        Catch e As Exception
            Console.WriteLine(e.Message.ToString())
        End Try
    End Sub 'FileIOPermissionAttributeDemo


    ' This method demonstrates the use of the FileIOPermissionAttribute
 to create a PermitOnly permission.
    ' Set the Read, PathDiscovery, Append, Write, and All properties.
    <FileIOPermissionAttribute(SecurityAction.PermitOnly, Read:="C:\"),
 _
    FileIOPermissionAttribute(SecurityAction.PermitOnly, _
    PathDiscovery:="C:\Documents and Settings\All Users"),
 _
    FileIOPermissionAttribute(SecurityAction.PermitOnly, _
    Append:="C:\Documents and Settings\All Users\Application Data"),
 _
    FileIOPermissionAttribute(SecurityAction.PermitOnly, _
        Write:="C:\Documents and Settings\All Users\Application
 Data\Microsoft"), _
    FileIOPermissionAttribute(SecurityAction.PermitOnly, _
        All:="C:\Documents and Settings\All Users\Application
 Data\Microsoft\Network")> _
    Public Shared Sub PermitOnlyMethod()
        Console.WriteLine("Executing PermitOnlyMethod.")
        Console.WriteLine("PermitOnly the Read permission for
 drive C.")
        Console.WriteLine("PermitOnly the PathDiscovery permission
 for " & ControlChars.Lf & ControlChars.Tab & "C:\Documents
 and Settings\All Users.")
        Console.WriteLine("PermitOnly the Append permission for
 " & ControlChars.Lf & ControlChars.Tab & "C:\Documents
 and Settings\All Users\Application Data.")
        Console.WriteLine("PermitOnly the Write permission for
  " & ControlChars.Lf & ControlChars.Tab & "C:\Documents
 and Settings\All Users\Application Data\Microsoft.")
        Console.WriteLine("PermitOnly the All permission for "
 & ControlChars.Lf & ControlChars.Tab & "C:\Documents
 and Settings\All Users\Application Data\Microsoft\Network.")

        PermitOnlyTestMethod()
    End Sub 'PermitOnlyMethod


    Public Shared Sub PermitOnlyTestMethod()
        Console.WriteLine("Executing PermitOnlyTestMethod.")
        Try
            Dim ps As New
 PermissionSet(PermissionState.None)
            ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.Write,
 "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\SomeFile"))
            Console.WriteLine(("Demanding permission to write
 " & "'C:\Documents and Settings\All Users\Application
 Data\Microsoft\Network\SomeFile'"))
            ps.Demand()
            Console.WriteLine("Demand succeeded.")
            ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.Write,
 "C:\"))
            Console.WriteLine("Demanding permission to write to
 drive C.")

            ' This demand should cause an exception.
            ps.Demand()
            ' The TestFailed method is called if an exception is not
 thrown.
            TestFailed()
        Catch e As Exception
            Console.WriteLine(("An exception was thrown because
 of a write demand: " & e.Message))
        End Try
    End Sub 'PermitOnlyTestMethod

    ' This method demonstrates the use of the FileIOPermission Attribute
 to deny a permission.
    <FileIOPermissionAttribute(SecurityAction.Deny, Write:="C:\Documents
 and Settings\All Users\Application Data\Microsoft\Network")> _
    Public Shared Sub DenyMethod()
        Console.WriteLine("Executing DenyMethod.")
        Console.WriteLine(("Denied the Write permission for "
 & ControlChars.Lf & ControlChars.Tab & "'C:\Documents
 and Settings\All Users\Application Data\Microsoft\Network'"))

        DenyTestMethod()
    End Sub 'DenyMethod


    Public Shared Sub DenyTestMethod()
        Console.WriteLine("Executing DenyTestMethod.")
        Try
            Dim ps As New
 PermissionSet(PermissionState.None)
            ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.Read,
 "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\SomeFile"))
            Console.WriteLine(("Demanding permission to read "
 & "'C:\Documents and Settings\All Users\Application Data\Microsoft\Network\SomeFile'"))
            ps.Demand()
            Console.WriteLine("Demand succeeded.")
            ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.Read
 Or FileIOPermissionAccess.Write Or FileIOPermissionAccess.Append,
 "C:\Documents and Settings\All Users\Application Data\Microsoft\Network\SomeFile"))
            Console.WriteLine(("Demanding permission to Read,
 Write, and Append to " & "'C:\Documents and Settings\All
 Users\Application Data\Microsoft\Network\SomeFile'"))

            ' This demand should cause an exception.
            ps.Demand()
            ' The TestFailed method is called if an exception is not
 thrown.
            TestFailed()
        Catch e As Exception

            Console.WriteLine(("An exception was thrown because
 of a write demand: " & e.Message))
        End Try
    End Sub 'DenyTestMethod

    ' This method demonstrates the use of the FileIOPermissionAttribute
 to deny all permissions.
    <FileIOPermissionAttribute(SecurityAction.Deny, Unrestricted:=True)> _
    Public Shared Sub DenyAllMethod()
        Console.WriteLine("Executing DenyAllMethod.")
        Console.WriteLine("Denied all FileIOPermissions")

        DenyAllTestMethod()
    End Sub 'DenyAllMethod

    ' This method tests to assure permissions have been denied.
    Public Shared Sub DenyAllTestMethod()
        Console.WriteLine("Executing DenyAllTestMethod.")
        Try
            Dim ps As New
 PermissionSet(PermissionState.None)
            ps.AddPermission(New FileIOPermission(FileIOPermissionAccess.Read,
 "C:\"))
            Console.WriteLine("Demanding permission to read
 'C:\'")
            ' This demand should cause an exception.
            ps.Demand()
            ' The TestFailed method is called if the expected exception
 is not thrown.
            TestFailed()

        Catch e As Exception

            Console.WriteLine(("An exception was thrown because
 of a read demand: " + e.Message))
        End Try
    End Sub 'DenyAllTestMethod

    Public Shared Sub TestFailed()
        Console.WriteLine("Executing TestFailed.")
        Console.WriteLine("Throwing an exception.")
        Throw New Exception()
    End Sub 'TestFailed

    Overloads Shared Sub
 Main(ByVal args() As String)
        FileIOPermissionAttributeDemo()
    End Sub 'Main
End Class '[MyClass] 

// This sample demonstrates the use of the FileIOPermissionAttribute
 class.
// The sample follows the recommended procedure of first granting PermitOnly
 permissions,
// then using a Deny on that set of granted permissions.

using System;
using System.Reflection;
using System.Security.Permissions;
using System.Security;
using System.IO;

class MyClass
{
    public static void FileIOPermissionAttributeDemo()
    {
        try
        {
            PermitOnlyMethod();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }
        try
        {
            DenyMethod();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }
        try
        {
            DenyAllMethod();
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message.ToString());
        }

    }

    // This method demonstrates the use of the FileIOPermissionAttribute
 to create a PermitOnly permission.
    // Set the Read property.
    [FileIOPermissionAttribute(SecurityAction.PermitOnly, Read = "C:\\")]
        // Set the PathDiscovery property.
    [FileIOPermissionAttribute(SecurityAction.PermitOnly,
         PathDiscovery = "C:\\Documents and Settings\\All Users")]
        // Set the Append property.
    [FileIOPermissionAttribute(SecurityAction.PermitOnly,
         Append = "C:\\Documents and Settings\\All Users\\Application Data")]
        // Set the Write property.
    [FileIOPermissionAttribute(SecurityAction.PermitOnly,
         Write = "C:\\Documents and Settings\\All Users\\Application Data\\Microsoft")]
        // Set the All property.
    [FileIOPermissionAttribute(SecurityAction.PermitOnly,
         All = "C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network")]
    public static void PermitOnlyMethod()
    {
        Console.WriteLine("Executing PermitOnlyMethod.");
        Console.WriteLine("PermitOnly the Read permission for
 drive C.");
        Console.WriteLine("PermitOnly the PathDiscovery permission for
 \n\tC:\\Documents and Settings\\All Users.");
        Console.WriteLine("PermitOnly the Append permission for
 \n\tC:\\Documents and Settings\\All Users\\Application Data.");
        Console.WriteLine("PermitOnly the Write permission for
 \n\tC:\\Documents and Settings\\All Users\\Application Data\\Microsoft.");
        Console.WriteLine("PermitOnly the All permission for
 \n\tC:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network.");

        PermitOnlyTestMethod();
    }

    public static void PermitOnlyTestMethod()
    {
        Console.WriteLine("Executing PermitOnlyTestMethod.");
        try
        {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write
,
                "C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile"));
            Console.WriteLine("Demanding permission to write " +
                "'C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile'");
            ps.Demand();
            Console.WriteLine("Demand succeeded.");
            ps.AddPermission(
                new FileIOPermission(FileIOPermissionAccess.Write
 ,
                "C:\\"));
            Console.WriteLine("Demanding permission to write to drive C.");

            // This demand should cause an exception.
            ps.Demand();
            // The TestFailed method is called if an exception is not
 thrown.
            TestFailed();
        }
        catch (Exception e)
        {
            Console.WriteLine("An exception was thrown because of a write demand:
 " + e.Message);
        }
    }
    // This method demonstrates the use of the FileIOPermission Attribute
 to deny a permission.
    [FileIOPermissionAttribute(SecurityAction.Deny,
         Write = "C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network")]
    public static void DenyMethod()
    {
        Console.WriteLine("Executing DenyMethod.");
        Console.WriteLine("Denied the Write permission for
 \n\t"
            + "'C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network'");

        DenyTestMethod();
    }

    public static void DenyTestMethod()
    {
        Console.WriteLine("Executing DenyTestMethod.");
        try
        {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission (new FileIOPermission(FileIOPermissionAccess.Read
,
                "C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile"));
            Console.WriteLine("Demanding permission to read " +
                "'C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile'");
            ps.Demand();
            Console.WriteLine("Demand succeeded.");
            ps.AddPermission(
                new FileIOPermission(FileIOPermissionAccess.Read
 |
                FileIOPermissionAccess.Write | FileIOPermissionAccess.Append,
                "C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile"));
            Console.WriteLine("Demanding permission to Read, Write, and Append
 to " +
                "'C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile'");

            // This demand should cause an exception.
            ps.Demand();
            // The TestFailed method is called if an exception is not
 thrown.
            TestFailed();
        }

        catch (Exception e)

        {
            Console.WriteLine("An exception was thrown because of a write demand:
 " + e.Message);
        }
    }

    // This method demonstrates the use of the FileIOPermissionAttribute
 to deny all permissions.
    [FileIOPermissionAttribute(SecurityAction.Deny, Unrestricted = true)]
    public static void DenyAllMethod()
    {
        Console.WriteLine("Executing DenyAllMethod.");
        Console.WriteLine("Denied all FileIOPermissions");

        DenyAllTestMethod();
    }
    // This method tests to assure permissions have been denied.
    public static void DenyAllTestMethod()
    {
        Console.WriteLine("Executing DenyAllTestMethod.");
        try
        {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission (new FileIOPermission(FileIOPermissionAccess.Read
, "C:\\"));
            Console.WriteLine("Demanding permission to read 'C:\\'");
            // This demand should cause an exception.
            ps.Demand();
            // The TestFailed method is called if the expected exception
 is not thrown.
            TestFailed();
        }

        catch (Exception e)

        {
            Console.WriteLine("An exception was thrown because of a read demand:
 " + e.Message);
        }
    }

    public static void TestFailed()
    {
        Console.WriteLine("Executing TestFailed.");
        Console.WriteLine("Throwing an exception.");
        throw new Exception();
    }

    static void Main(string[]
 args)
    {
        FileIOPermissionAttributeDemo();
    }

}

// This sample demonstrates the use of the FileIOPermissionAttribute
 class.
// The sample follows the recommended procedure of first granting PermitOnly
 permissions, 
// then using a Deny on that set of granted permissions.  
using namespace System;
using namespace System::Reflection;
using namespace System::Security::Permissions;
using namespace System::Security;
using namespace System::IO;
void PermitOnlyMethod();
void DenyMethod();
void DenyAllMethod();
void PermitOnlyTestMethod();
void TestFailed();
void DenyTestMethod();
void DenyAllTestMethod();
void FileIOPermissionAttributeDemo()
{
   try
   {
      PermitOnlyMethod();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

   try
   {
      DenyMethod();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

   try
   {
      DenyAllMethod();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}


// This method demonstrates the use of the FileIOPermissionAttribute
 to create a PermitOnly permission.
// Set the Read property.
[FileIOPermissionAttribute(SecurityAction::PermitOnly,Read="C:\\")]
// Set the PathDiscovery property.
[FileIOPermissionAttribute(SecurityAction::PermitOnly,
PathDiscovery="C:\\Documents and Settings\\All Users")]
// Set the Append property.
[FileIOPermissionAttribute(SecurityAction::PermitOnly,
Append="C:\\Documents and Settings\\All Users\\Application Data")]
// Set the Write property.
[FileIOPermissionAttribute(SecurityAction::PermitOnly,
Write="C:\\Documents and Settings\\All Users\\Application Data\\Microsoft")]
// Set the All property.
[FileIOPermissionAttribute(SecurityAction::PermitOnly,
All="C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network")]

void PermitOnlyMethod()
{
   Console::WriteLine( "Executing PermitOnlyMethod." );
   Console::WriteLine( "PermitOnly the Read permission for
 drive C." );
   Console::WriteLine( "PermitOnly the PathDiscovery permission for
 \n\tC:\\Documents and Settings\\All Users." );
   Console::WriteLine( "PermitOnly the Append permission for
 \n\tC:\\Documents and Settings\\All Users\\Application Data." );
   Console::WriteLine( "PermitOnly the Write permission for
  \n\tC:\\Documents and Settings\\All Users\\Application Data\\Microsoft."
 );
   Console::WriteLine( "PermitOnly the All permission for
 \n\tC:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network."
 );
   PermitOnlyTestMethod();
}

void PermitOnlyTestMethod()
{
   Console::WriteLine( "Executing PermitOnlyTestMethod." );
   try
   {
      PermissionSet^ ps = gcnew PermissionSet( PermissionState::None );
      ps->AddPermission( gcnew FileIOPermission( FileIOPermissionAccess::Write,"C:\\Documents
 and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile" )
 );
      Console::WriteLine( "Demanding permission to write "
      "'C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile'"
 );
      ps->Demand();
      Console::WriteLine( "Demand succeeded." );
      ps->AddPermission( gcnew FileIOPermission( FileIOPermissionAccess::Write,"C:\\"
 ) );
      Console::WriteLine( "Demanding permission to write to drive C." );
      
      // This demand should cause an exception.
      ps->Demand();
      
      // The TestFailed method is called if an exception is not thrown.
      TestFailed();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "An exception was thrown because of a write demand:
 {0}", e->Message );
   }

}


// This method demonstrates the use of the FileIOPermission Attribute
 to deny a permission.

[FileIOPermissionAttribute(SecurityAction::Deny,
Write="C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network")]
void DenyMethod()
{
   Console::WriteLine( "Executing DenyMethod." );
   Console::WriteLine( "Denied the Write permission for \n\t"
   "'C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network'"
 );
   DenyTestMethod();
}

void DenyTestMethod()
{
   Console::WriteLine( "Executing DenyTestMethod." );
   try
   {
      PermissionSet^ ps = gcnew PermissionSet( PermissionState::None );
      ps->AddPermission( gcnew FileIOPermission( FileIOPermissionAccess::Read,"C:\\Documents
 and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile" )
 );
      Console::WriteLine( "Demanding permission to read "
      "'C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile'"
 );
      ps->Demand();
      Console::WriteLine( "Demand succeeded." );
      ps->AddPermission( gcnew FileIOPermission( static_cast<FileIOPermissionAccess>(FileIOPermissionAccess::Read
 | FileIOPermissionAccess::Write | FileIOPermissionAccess::Append),"C:\\Documents
 and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile" ) );
      Console::WriteLine( "Demanding permission to Read, Write, and Append to
 "
      "'C:\\Documents and Settings\\All Users\\Application Data\\Microsoft\\Network\\SomeFile'"
 );
      
      // This demand should cause an exception.
      ps->Demand();
      
      // The TestFailed method is called if an exception is not thrown.
      TestFailed();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "An exception was thrown because of a write demand:
 {0}", e->Message );
   }

}


// This method demonstrates the use of the FileIOPermissionAttribute
 to deny all permissions.

[FileIOPermissionAttribute(SecurityAction::Deny,Unrestricted=true)]
void DenyAllMethod()
{
   Console::WriteLine( "Executing DenyAllMethod." );
   Console::WriteLine( "Denied all FileIOPermissions" );
   DenyAllTestMethod();
}


// This method tests to assure permissions have been denied.
void DenyAllTestMethod()
{
   Console::WriteLine( "Executing DenyAllTestMethod." );
   try
   {
      PermissionSet^ ps = gcnew PermissionSet( PermissionState::None );
      ps->AddPermission( gcnew FileIOPermission( FileIOPermissionAccess::Read,"C:\\"
 ) );
      Console::WriteLine( "Demanding permission to read 'C:\\'" );
      
      // This demand should cause an exception.
      ps->Demand();
      
      // The TestFailed method is called if the expected exception is
 not thrown.
      TestFailed();
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "An exception was thrown because of a read demand:
 {0}", e->Message );
   }

}

void TestFailed()
{
   Console::WriteLine( "Executing TestFailed." );
   Console::WriteLine( "Throwing an exception." );
   throw gcnew Exception;
}

int main()
{
   FileIOPermissionAttributeDemo();
}

// This sample demonstrates the use of the FileIOPermissionAttribute
 class.
// The sample follows the recommended procedure of first granting 
// PermitOnly permissions,
// then using a Deny on that set of granted permissions.
import System.*;
import System.Reflection.*;
import System.Security.Permissions.*;
import System.Security.*;
import System.IO.*;

class MyClass
{
    public static void FileIOPermissionAttributeDemo()
    {
        try {
            PermitOnlyMethod();
        }
        catch (System.Exception e) {
            Console.WriteLine(e.get_Message().toString());
        }
        try {
            DenyMethod();
        }
        catch (System.Exception e) {
            Console.WriteLine(e.get_Message().toString()); 
        }
        try {
            DenyAllMethod();
        }
        catch (System.Exception e) {
            Console.WriteLine(e.get_Message().toString());
        }
    } //FileIOPermissionAttributeDemo

    // This method demonstrates the use of the FileIOPermissionAttribute
 
    // to create a PermitOnly permission.
    // Set the Read property.
    /** @attribute FileIOPermissionAttribute(SecurityAction.PermitOnly, 
        Read = "C:\\")
     */
    
    // Set the PathDiscovery property.
    /** @attribute FileIOPermissionAttribute(SecurityAction.PermitOnly, 
        PathDiscovery = "C:\\Documents and Settings\\All Users")
     */

    // Set the Append property.
    /** @attribute FileIOPermissionAttribute(SecurityAction.PermitOnly, 
        Append = "C:\\Documents and Settings\\All Users\\Application Data")
     */
    
    // Set the Write property.
    /** @attribute FileIOPermissionAttribute(SecurityAction.PermitOnly, 
        Write = "C:\\Documents and Settings\\All Users " + 
        "\\Application Data\\Microsoft")
     */
    
    // Set the All property.
    /** @attribute FileIOPermissionAttribute(SecurityAction.PermitOnly, 
        All = "C:\\Documents and Settings\\" +
        "All Users\\Application Data\\Microsoft\\Network")
     */

    public static void PermitOnlyMethod()
    {
        Console.WriteLine("Executing PermitOnlyMethod.");
        Console.WriteLine("PermitOnly the Read permission for
 drive C.");
        Console.WriteLine("PermitOnly the PathDiscovery permission " 
            + "for \n\tC:\\Documents and Settings\\All Users.");
        Console.WriteLine("PermitOnly the Append permission for
 \n\tC:\\" 
            + "Documents and Settings\\All Users\\Application Data.");
        Console.WriteLine("PermitOnly the Write permission for
 \n\tC:\\" 
            + "Documents and Settings\\All Users\\Application Data" 
            + "\\Microsoft.");
        Console.WriteLine("PermitOnly the All permission for
 \n\tC:\\" 
            + "Documents and Settings\\All Users\\Application Data\\Microsoft"
 
            + "\\Network.");
        PermitOnlyTestMethod();
    } //PermitOnlyMethod

    public static void PermitOnlyTestMethod()
    {
        Console.WriteLine("Executing PermitOnlyTestMethod.");
        try {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new FileIOPermission(
                FileIOPermissionAccess.Write, "C:\\Documents and Settings"
 
                + "\\All Users\\Application Data\\Microsoft\\Network" 
                + "\\SomeFile"));
            Console.WriteLine(("Demanding permission to write " 
                + "'C:\\Documents and Settings\\All Users\\Application Data"
 
                + "\\Microsoft\\Network\\SomeFile'"));
            ps.Demand();
            Console.WriteLine("Demand succeeded.");
            ps.AddPermission(new FileIOPermission(FileIOPermissionAccess.Write
, 
                "C:\\"));
            Console.WriteLine("Demanding permission to write to drive C.");

            // This demand should cause an exception.
            ps.Demand();

            // The TestFailed method is called if an exception is not
 thrown.
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("An exception was thrown because of " 
                + "a write demand: " + e.get_Message()));
        }
    } //PermitOnlyTestMethod

    // This method demonstrates the use of the FileIOPermission Attribute
 
    // to deny a permission.
    /** @attribute FileIOPermissionAttribute(SecurityAction.Deny, 
        Write = "C:\\Documents and Settings\\All Users\\Application Data\\"
 
        + "Microsoft\\Network")
     */
    public static void DenyMethod()
    {
        Console.WriteLine("Executing DenyMethod.");
        Console.WriteLine(("Denied the Write permission for
 \n\t" 
            + "'C:\\Documents and Settings\\All Users\\Application Data"
 
            + "\\Microsoft\\Network'"));
        DenyTestMethod();
    } //DenyMethod

    public static void DenyTestMethod()
    {
        Console.WriteLine("Executing DenyTestMethod.");
        try {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new FileIOPermission(
                FileIOPermissionAccess.Read, "C:\\Documents and Settings"
 
                + "\\All Users\\Application Data\\Microsoft\\Network"
                + "\\SomeFile"));
            Console.WriteLine(("Demanding permission to read " 
                + "'C:\\Documents and Settings\\All Users\\Application "
 
                + "Data\\Microsoft\\Network\\SomeFile'"));
            ps.Demand();
            Console.WriteLine("Demand succeeded.");
            ps.AddPermission(new FileIOPermission(
                FileIOPermissionAccess.Read | FileIOPermissionAccess.Write 
                | FileIOPermissionAccess.Append, 
                "C:\\Documents and Settings\\All Users\\Application " 
                + "Data\\Microsoft\\Network\\SomeFile"));
            Console.WriteLine(("Demanding permission to Read, " 
                + "Write, and Append to " 
                + "'C:\\Documents and Settings\\All Users\\Application "
 
                + "Data\\Microsoft\\Network\\SomeFile'"));

            // This demand should cause an exception.
            ps.Demand();

            // The TestFailed method is called if an exception is not
 thrown.
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("An exception was thrown because of a "
 
                + "write demand: " + e.get_Message()));
        }
    } //DenyTestMethod   

    // This method demonstrates the use of the FileIOPermissionAttribute
 
    // to deny all permissions.
    /** @attribute FileIOPermissionAttribute(SecurityAction.Deny, 
        Unrestricted = true)
     */
    public static void DenyAllMethod()
    {
        Console.WriteLine("Executing DenyAllMethod.");
        Console.WriteLine("Denied all FileIOPermissions");
        DenyAllTestMethod();
    } //DenyAllMethod

    // This method tests to assure permissions have been denied.
    public static void DenyAllTestMethod()
    {
        Console.WriteLine("Executing DenyAllTestMethod.");
        try {
            PermissionSet ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new FileIOPermission(
                FileIOPermissionAccess.Read, "C:\\"));
            Console.WriteLine("Demanding permission to read 'C:\\'");

            // This demand should cause an exception.
            ps.Demand();

            // The TestFailed method is called if the expected exception
 
            //is not thrown.
            TestFailed();
        }
        catch (System.Exception e) {
            Console.WriteLine(("An exception was thrown because of a "
 
                + "read demand: " + e.get_Message()));
        }
    } //DenyAllTestMethod

    public static void TestFailed()
 throws Exception
    {
        Console.WriteLine("Executing TestFailed.");
        Console.WriteLine("Throwing an exception.");
        throw new Exception();
    } //TestFailed

    public static void main(String[]
 args)
    {
        FileIOPermissionAttributeDemo();
    } //main
} //MyClass
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
FileIOPermissionAttribute クラス
FileIOPermissionAttribute メンバ
System.Security.Permissions 名前空間

FileIOPermissionAttribute プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ Action  セキュリティ アクションを取得または設定します。 ( SecurityAttribute から継承されます。)
パブリック プロパティ All 文字列値が指定するファイルまたはディレクトリへのフル アクセスを設定します。
パブリック プロパティ AllFiles すべてのファイルに対して許可されているアクセスを取得または設定します。
パブリック プロパティ AllLocalFiles すべてのローカル ファイルに対して許可されているアクセスを取得または設定します。
パブリック プロパティ Append 文字列値が指定するファイルまたはディレクトリへの追加アクセスを取得または設定します。
パブリック プロパティ ChangeAccessControl アクセス制御情報を変更できるファイルまたはディレクトリを取得または設定します。
パブリック プロパティ PathDiscovery パス探索を与えるファイルまたはディレクトリを取得または設定します。
パブリック プロパティ Read 文字列値が指定するファイルまたはディレクトリへの読み取りアクセスを取得または設定します。
パブリック プロパティ TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。 ( Attribute から継承されます。)
パブリック プロパティ Unrestricted  属性によって保護されているリソースに対して完全な (無制限の) アクセス許可が宣言されているかどうかを示す値を取得または設定します。 ( SecurityAttribute から継承されます。)
パブリック プロパティ ViewAccessControl アクセス制御情報を表示できるファイルまたはディレクトリを取得または設定します。
パブリック プロパティ ViewAndModify ファイル データを表示して変更できるファイルまたはディレクトリを取得または設定します。
パブリック プロパティ Write 文字列値が指定するファイルまたはディレクトリへの書き込みアクセスを取得または設定します。
参照参照

関連項目

FileIOPermissionAttribute クラス
System.Security.Permissions 名前空間
FileIOPermission クラス
FileIOPermissionAccess 列挙体

その他の技術情報

属性を使用したメタデータの拡張

FileIOPermissionAttribute メソッド


パブリック メソッドパブリック メソッド

  名前 説明
パブリック メソッド CreatePermission オーバーライドされます。 新しい FileIOPermission を作成して返します。
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 ( Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスのハッシュ コードを返します。 ( Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 ( Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 ( Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 ( Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 ( Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 ( Object から継承されます。)
参照参照

関連項目

FileIOPermissionAttribute クラス
System.Security.Permissions 名前空間
FileIOPermission クラス
FileIOPermissionAccess 列挙体

その他の技術情報

属性を使用したメタデータの拡張

FileIOPermissionAttribute メンバ

宣言セキュリティを使用して、FileIOPermission のセキュリティ アクションをコードに適用できるようにします。このクラスは継承できません。

FileIOPermissionAttribute データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド FileIOPermissionAttribute SecurityAction を指定して、FileIOPermissionAttribute クラスの新しいインスタンスを初期化します。
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ Action  セキュリティ アクションを取得または設定します。(SecurityAttribute から継承されます。)
パブリック プロパティ All 文字列値が指定するファイルまたはディレクトリへのフル アクセスを設定します。
パブリック プロパティ AllFiles すべてのファイルに対して許可されているアクセスを取得または設定します。
パブリック プロパティ AllLocalFiles すべてのローカル ファイルに対して許可されているアクセスを取得または設定します。
パブリック プロパティ Append 文字列値が指定するファイルまたはディレクトリへの追加アクセスを取得または設定します。
パブリック プロパティ ChangeAccessControl アクセス制御情報を変更できるファイルまたはディレクトリを取得または設定します。
パブリック プロパティ PathDiscovery パス探索を与えるファイルまたはディレクトリを取得または設定します。
パブリック プロパティ Read 文字列値が指定するファイルまたはディレクトリへの読み取りアクセスを取得または設定します。
パブリック プロパティ TypeId  派生クラスに実装されている場合は、この Attribute の一意の識別子を取得します。(Attribute から継承されます。)
パブリック プロパティ Unrestricted  属性によって保護されているリソースに対して完全な (無制限の) アクセス許可が宣言されているかどうかを示す値を取得または設定します。(SecurityAttribute から継承されます。)
パブリック プロパティ ViewAccessControl アクセス制御情報を表示できるファイルまたはディレクトリを取得または設定します。
パブリック プロパティ ViewAndModify ファイル データを表示して変更できるファイルまたはディレクトリを取得または設定します。
パブリック プロパティ Write 文字列値が指定するファイルまたはディレクトリへの書き込みアクセスを取得または設定します。
パブリック メソッドパブリック メソッド
  名前 説明
パブリック メソッド CreatePermission オーバーライドされます。 新しい FileIOPermission を作成して返します。
パブリック メソッド Equals  オーバーロードされます。 ( Attribute から継承されます。)
パブリック メソッド GetCustomAttribute  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用された指定した型のカスタム属性を取得します。 (Attribute から継承されます。)
パブリック メソッド GetCustomAttributes  オーバーロードされます。 アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されたカスタム属性の配列を取得します。 (Attribute から継承されます。)
パブリック メソッド GetHashCode  このインスタンスのハッシュ コードを返します。 (Attribute から継承されます。)
パブリック メソッド GetType  現在のインスタンスの Type を取得します。 (Object から継承されます。)
パブリック メソッド IsDefaultAttribute  派生クラス内でオーバーライドされたときに、このインスタンスの値が派生クラスの既定値かどうかを示します。 (Attribute から継承されます。)
パブリック メソッド IsDefined  オーバーロードされます。 指定した型のカスタム属性が、アセンブリ、モジュール、型のメンバ、またはメソッド パラメータに適用されているかどうかを判断します。 (Attribute から継承されます。)
パブリック メソッド Match  派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。 (Attribute から継承されます。)
パブリック メソッド ReferenceEquals  指定した複数の Object インスタンスが同一かどうかを判断します。 (Object から継承されます。)
パブリック メソッド ToString  現在の Object を表す String を返します。 (Object から継承されます。)
参照参照

関連項目

FileIOPermissionAttribute クラス
System.Security.Permissions 名前空間
FileIOPermission クラス
FileIOPermissionAccess 列挙体

その他の技術情報

属性を使用したメタデータの拡張



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「FileIOPermissionAttribute」の関連用語

FileIOPermissionAttributeのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FileIOPermissionAttributeのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS