UnauthorizedAccessException Klasse

Definition

Die Ausnahme, die ausgelöst wird, wenn das Betriebssystem aufgrund eines E/A-Fehlers oder eines bestimmten Typs von Sicherheitsfehler den Zugriff verweigert.

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
Vererbung
UnauthorizedAccessException
Vererbung
UnauthorizedAccessException
Abgeleitet
Attribute

Beispiele

Im folgenden Beispiel wird die Ausnahme veranschaulicht, die UnauthorizedAccessException beim Schreiben in eine schreibgeschützte Datei ausgelöst wird.

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.

Hinweise

Eine UnauthorizedAccessException Ausnahme wird in der Regel von einer Methode ausgelöst, die einen Windows API-Aufruf umschließt. Um die Gründe für die Ausnahme zu finden, überprüfen Sie den Text der Eigenschaft des Ausnahmeobjekts Message .

UnauthorizedAccessException verwendet den HRESULT COR_E_UNAUTHORIZEDACCESSWert 0x80070005.

Konstruktoren

UnauthorizedAccessException()

Initialisiert eine neue Instanz der UnauthorizedAccessException-Klasse.

UnauthorizedAccessException(SerializationInfo, StreamingContext)

Initialisiert eine neue Instanz der UnauthorizedAccessException-Klasse mit serialisierten Daten.

UnauthorizedAccessException(String)

Initialisiert eine neue Instanz der UnauthorizedAccessException-Klasse mit einer angegebenen Fehlermeldung.

UnauthorizedAccessException(String, Exception)

Initialisiert eine neue Instanz der UnauthorizedAccessException-Klasse mit einer angegebenen Fehlermeldung und einem Verweis auf die innere Ausnahme, die diese Ausnahme ausgelöst hat.

Eigenschaften

Data

Ruft eine Auflistung von Schlüssel-Wert-Paaren ab, die zusätzliche benutzerdefinierte Informationen zur Ausnahme bereitstellen.

(Geerbt von Exception)
HelpLink

Ruft einen Link zur Hilfedatei ab, die dieser Ausnahme zugeordnet ist, oder legt einen Link fest.

(Geerbt von Exception)
HResult

Ruft HRESULT ab oder legt HRESULT fest. Dies ist ein codierter Wert, der einer bestimmten Ausnahme zugeordnet ist.

(Geerbt von Exception)
InnerException

Ruft die Exception-Instanz ab, die die aktuelle Ausnahme verursacht hat.

(Geerbt von Exception)
Message

Ruft eine Meldung ab, mit der die aktuelle Ausnahme beschrieben wird.

(Geerbt von Exception)
Source

Gibt den Namen der Anwendung oder des Objekts zurück, die bzw. das den Fehler verursacht hat, oder legt diesen fest.

(Geerbt von Exception)
StackTrace

Ruft eine Zeichenfolgendarstellung der unmittelbaren Frames in der Aufrufliste ab.

(Geerbt von Exception)
TargetSite

Ruft die Methode ab, die die aktuelle Ausnahme auslöst.

(Geerbt von Exception)

Methoden

Equals(Object)

Bestimmt, ob das angegebene Objekt gleich dem aktuellen Objekt ist.

(Geerbt von Object)
GetBaseException()

Gibt beim Überschreiben in einer abgeleiteten Klasse eine Exception zurück, die die Grundursache für eine oder mehrere nachfolgende Ausnahmen ist.

(Geerbt von Exception)
GetHashCode()

Fungiert als Standardhashfunktion.

(Geerbt von Object)
GetObjectData(SerializationInfo, StreamingContext)

Legt beim Überschreiben in einer abgeleiteten Klasse die SerializationInfo mit Informationen über die Ausnahme fest.

(Geerbt von Exception)
GetType()

Ruft den Laufzeittyp der aktuellen Instanz ab.

(Geerbt von Exception)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.

(Geerbt von Object)
ToString()

Erstellt eine Zeichenfolgendarstellung der aktuellen Ausnahme und gibt diese zurück.

(Geerbt von Exception)

Ereignisse

SerializeObjectState
Veraltet.

Tritt auf, wenn eine Ausnahme serialisiert wird, um ein Ausnahmezustandsobjekt mit serialisierten Daten über die Ausnahme zu erstellen.

(Geerbt von Exception)

Gilt für

Siehe auch