Partage via


UnauthorizedAccessException Classe

Définition

Exception levée lorsque le système d’exploitation refuse l’accès en raison d’une erreur d’E/S ou d’un type spécifique d’erreur de sécurité.

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 exception UnauthorizedAccessException 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é Message de l’objet d’exception.

UnauthorizedAccessException utilise le HRESULTCOR_E_UNAUTHORIZEDACCESS, qui a la valeur 0x80070005.

Constructeurs

UnauthorizedAccessException()

Initialise une nouvelle instance de la classe UnauthorizedAccessException.

UnauthorizedAccessException(SerializationInfo, StreamingContext)
Obsolète.

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 qui est la cause de cette exception.

Propriétés

Data

Obtient une collection de paires clé/valeur qui fournissent des informations supplémentaires définies par l’utilisateur 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 affecté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 actuelle.

(Hérité de Exception)
Source

Obtient ou définit le nom de l’application ou de l’objet qui provoque l’erreur.

(Hérité de Exception)
StackTrace

Obtient une représentation sous forme de chaîne des images immédiates 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 la cause racine d’une ou plusieurs exceptions ultérieures.

(Hérité de Exception)
GetHashCode()

Sert de fonction de hachage par défaut.

(Hérité de Object)
GetObjectData(SerializationInfo, StreamingContext)
Obsolète.

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

(Hérité de Exception)
GetType()

Obtient le type d’exécution de l’instance actuelle.

(Hérité de Exception)
MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
ToString()

Crée et retourne une représentation sous forme de chaîne de l’exception actuelle.

(Hérité de Exception)

Événements

SerializeObjectState
Obsolète.

Se produit lorsqu’une exception est sérialisée pour créer un objet d’état d’exception qui contient des données sérialisées sur l’exception.

(Hérité de Exception)

S’applique à

Voir aussi