UnauthorizedAccessException Klas

Definitie

De uitzondering die wordt gegenereerd wanneer het besturingssysteem de toegang weigert vanwege een I/O-fout of een specifiek type beveiligingsfout.

public ref class UnauthorizedAccessException : Exception
public ref class UnauthorizedAccessException : SystemException
public class UnauthorizedAccessException : Exception
public class UnauthorizedAccessException : SystemException
[System.Serializable]
public class UnauthorizedAccessException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class UnauthorizedAccessException : SystemException
type UnauthorizedAccessException = class
    inherit Exception
type UnauthorizedAccessException = class
    inherit SystemException
[<System.Serializable>]
type UnauthorizedAccessException = class
    inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type UnauthorizedAccessException = class
    inherit SystemException
Public Class UnauthorizedAccessException
Inherits Exception
Public Class UnauthorizedAccessException
Inherits SystemException
Overname
UnauthorizedAccessException
Overname
UnauthorizedAccessException
Afgeleid
Kenmerken

Voorbeelden

In het volgende voorbeeld ziet u de UnauthorizedAccessException uitzondering die wordt gegenereerd bij het schrijven naar een bestand met het kenmerk Alleen-lezen.

using System;
using System.IO;

public class Example
{
   public static void Main()
   {
      string filePath = @".\ROFile.txt";
      if (!File.Exists(filePath))
         File.Create(filePath);
      // Keep existing attributes, and set ReadOnly attribute.
      File.SetAttributes(filePath, 
                        (new FileInfo(filePath)).Attributes | FileAttributes.ReadOnly);

      StreamWriter sw = null;
      try {
         sw = new StreamWriter(filePath);
         sw.Write("Test");
      }
      catch (UnauthorizedAccessException) {
         FileAttributes attr = (new FileInfo(filePath)).Attributes;
         Console.Write("UnAuthorizedAccessException: Unable to access file. ");
         if ((attr & FileAttributes.ReadOnly) > 0)
            Console.Write("The file is read-only."); 
       }
       finally {
         if (sw != null) sw.Close();
      }   
   }
}
// The example displays the following output:
//    UnAuthorizedAccessException: Unable to access file. The file is read-only.
open System
open System.IO


let filePath = @".\ROFile.txt"
if File.Exists filePath |> not then
    File.Create filePath |> ignore
// Keep existing attributes, and set ReadOnly attribute.
File.SetAttributes(filePath, (FileInfo filePath).Attributes ||| FileAttributes.ReadOnly)

do
    use sw = new StreamWriter(filePath)
    try
        sw.Write "Test"
    with :? UnauthorizedAccessException ->
        let attr = (FileInfo filePath).Attributes
        printf "UnAuthorizedAccessException: Unable to access file. "
        if int (attr &&& FileAttributes.ReadOnly) > 0 then
            printf "The file is read-only."
// The example displays the following output:
//    UnAuthorizedAccessException: Unable to access file. The file is read-only.
Imports System.IO

Module Example
   Public Sub Main()
      Dim filePath As String = ".\ROFile.txt"
      If Not File.Exists(filePath) Then File.Create(filePath)
      ' Keep existing attributes, and set ReadOnly attribute.
      File.SetAttributes(filePath, 
                        (New FileInfo(filePath)).Attributes Or FileAttributes.ReadOnly)

      Dim sw As StreamWriter = Nothing
      Try
         sw = New StreamWriter(filePath)
         sw.Write("Test")
      Catch e As UnauthorizedAccessException
         Dim attr As FileAttributes = (New FileInfo(filePath)).Attributes
         Console.Write("UnAuthorizedAccessException: Unable to access file. ")
         If (attr And FileAttributes.ReadOnly) > 0 Then
            Console.Write("The file is read-only.") 
         End If
       Finally
         If sw IsNot Nothing Then sw.Close()
      End Try   
   End Sub
End Module
' The example displays the following output:
'   UnAuthorizedAccessException: Unable to access file. The file is read-only.

Opmerkingen

Een UnauthorizedAccessException uitzondering wordt meestal gegenereerd door een methode waarmee een Windows API-aanroep wordt verpakt. Als u de redenen voor de uitzondering wilt achterhalen, bekijkt u de tekst van de eigenschap van het uitzonderingsobject Message .

UnauthorizedAccessException gebruikt de HRESULTCOR_E_UNAUTHORIZEDACCESS, die de waarde heeft 0x80070005.

Constructors

Name Description
UnauthorizedAccessException()

Initialiseert een nieuw exemplaar van de UnauthorizedAccessException klasse.

UnauthorizedAccessException(SerializationInfo, StreamingContext)
Verouderd.

Initialiseert een nieuw exemplaar van de UnauthorizedAccessException klasse met geserialiseerde gegevens.

UnauthorizedAccessException(String, Exception)

Initialiseert een nieuw exemplaar van de UnauthorizedAccessException klasse met een opgegeven foutbericht en een verwijzing naar de binnenste uitzondering die de oorzaak van deze uitzondering is.

UnauthorizedAccessException(String)

Initialiseert een nieuw exemplaar van de UnauthorizedAccessException klasse met een opgegeven foutbericht.

Eigenschappen

Name Description
Data

Hiermee haalt u een verzameling sleutel-waardeparen op die aanvullende door de gebruiker gedefinieerde informatie over de uitzondering bieden.

(Overgenomen van Exception)
HelpLink

Hiermee haalt u een koppeling op naar het Help-bestand dat aan deze uitzondering is gekoppeld.

(Overgenomen van Exception)
HResult

Hiermee wordt HRESULT opgehaald of ingesteld, een gecodeerde numerieke waarde die is toegewezen aan een specifieke uitzondering.

(Overgenomen van Exception)
InnerException

Hiermee haalt u het Exception exemplaar op dat de huidige uitzondering heeft veroorzaakt.

(Overgenomen van Exception)
Message

Hiermee wordt een bericht weergegeven waarin de huidige uitzondering wordt beschreven.

(Overgenomen van Exception)
Source

Hiermee wordt de naam van de toepassing of het object dat de fout veroorzaakt, opgehaald of ingesteld.

(Overgenomen van Exception)
StackTrace

Hiermee haalt u een tekenreeksweergave van de directe frames op de aanroepstack op.

(Overgenomen van Exception)
TargetSite

Hiermee haalt u de methode op waarmee de huidige uitzondering wordt gegenereerd.

(Overgenomen van Exception)

Methoden

Name Description
Equals(Object)

Bepaalt of het opgegeven object gelijk is aan het huidige object.

(Overgenomen van Object)
GetBaseException()

Wanneer deze wordt overschreven in een afgeleide klasse, retourneert u de Exception hoofdoorzaak van een of meer volgende uitzonderingen.

(Overgenomen van Exception)
GetHashCode()

Fungeert als de standaardhashfunctie.

(Overgenomen van Object)
GetObjectData(SerializationInfo, StreamingContext)
Verouderd.

Wanneer deze wordt overschreven in een afgeleide klasse, stelt u de SerializationInfo met informatie over de uitzondering in.

(Overgenomen van Exception)
GetType()

Hiermee haalt u het runtimetype van het huidige exemplaar op.

(Overgenomen van Exception)
MemberwiseClone()

Hiermee maakt u een ondiepe kopie van de huidige Object.

(Overgenomen van Object)
ToString()

Hiermee maakt en retourneert u een tekenreeksweergave van de huidige uitzondering.

(Overgenomen van Exception)

gebeurtenis

Name Description
SerializeObjectState
Verouderd.

Treedt op wanneer een uitzondering wordt geserialiseerd om een uitzonderingsstatusobject te maken dat geserialiseerde gegevens over de uitzondering bevat.

(Overgenomen van Exception)

Van toepassing op

Zie ook