UnauthorizedAccessException Classe

Définition

Exception levée quand le système d'exploitation refuse l'accès suite à une erreur d'E/S ou à un type d'erreur de sécurité spécifique.

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
Héritage
UnauthorizedAccessException
Héritage
UnauthorizedAccessException
Dérivé
Attributs

Exemples

L’exemple suivant illustre l’exception UnauthorizedAccessException levée lors de la tentative d’écriture dans un fichier en lecture seule.

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.

Remarques

Une UnauthorizedAccessException exception est généralement levée par une méthode qui encapsule un appel d’API Windows. Pour trouver les raisons de l’exception, examinez le texte de la propriété de Message l’objet exception.

UnauthorizedAccessException utilise le HRESULT COR_E_UNAUTHORIZEDACCESS, qui a la valeur 0x80070005.

Constructeurs

UnauthorizedAccessException()

Initialise une nouvelle instance de la classe UnauthorizedAccessException.

UnauthorizedAccessException(SerializationInfo, StreamingContext)

Initialise une nouvelle instance de la classe UnauthorizedAccessException avec des données sérialisées.

UnauthorizedAccessException(String)

Initialise une nouvelle instance de la classe UnauthorizedAccessException avec un message d'erreur spécifié.

UnauthorizedAccessException(String, Exception)

Initialise une nouvelle instance de la classe UnauthorizedAccessException avec un message d'erreur spécifié et une référence à l'exception interne ayant provoqué cette exception.

Propriétés

Data

Obtient une collection de paires clé/valeur qui fournissent des informations définies par l'utilisateur supplémentaires sur l'exception.

(Hérité de Exception)
HelpLink

Obtient ou définit un lien vers le fichier d'aide associé à cette exception.

(Hérité de Exception)
HResult

Obtient ou définit HRESULT, valeur numérique codée qui est assignée à une exception spécifique.

(Hérité de Exception)
InnerException

Obtient l'instance Exception qui a provoqué l'exception actuelle.

(Hérité de Exception)
Message

Obtient un message qui décrit l'exception active.

(Hérité de Exception)
Source

Obtient ou définit le nom de l'application ou de l'objet qui est à l'origine de l'erreur.

(Hérité de Exception)
StackTrace

Obtient une représentation sous forme de chaîne des frames immédiats sur la pile des appels.

(Hérité de Exception)
TargetSite

Obtient la méthode qui lève l'exception actuelle.

(Hérité de Exception)

Méthodes

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetBaseException()

En cas de substitution dans une classe dérivée, retourne la Exception qui est à l'origine d'une ou de plusieurs exceptions ultérieures.

(Hérité de Exception)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetObjectData(SerializationInfo, StreamingContext)

En cas de substitution dans une classe dérivée, définit SerializationInfo avec des informations sur l'exception.

(Hérité de Exception)
GetType()

Obtient le type au moment de l'exécution de l'instance actuelle.

(Hérité de Exception)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
ToString()

Crée et retourne une chaîne représentant l'exception actuelle.

(Hérité de Exception)

Événements

SerializeObjectState
Obsolète.

Se produit quand une exception est sérialisée pour créer un objet d'état d'exception qui contient des données sérialisées concernant l'exception.

(Hérité de Exception)

S’applique à

Voir aussi