UnauthorizedAccessException Classe

Definizione

Eccezione generata quando il sistema operativo nega l'accesso a causa di un errore I/O o di un tipo specifico di errore di sicurezza.

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
Ereditarietà
UnauthorizedAccessException
Ereditarietà
UnauthorizedAccessException
Derivato
Attributi

Esempio

Nell'esempio seguente viene illustrata l'eccezione UnauthorizedAccessException generata quando si tenta di scrivere in un file di sola lettura.

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.

Commenti

Un'eccezione UnauthorizedAccessException viene in genere generata da un metodo che esegue il wrapping di una chiamata all'API di Windows. Per trovare i motivi dell'eccezione, esaminare il testo della proprietà Message dell'oggetto eccezione.

UnauthorizedAccessException usa , HRESULT COR_E_UNAUTHORIZEDACCESSche ha il valore 0x80070005.

Costruttori

UnauthorizedAccessException()

Inizializza una nuova istanza della classe UnauthorizedAccessException.

UnauthorizedAccessException(SerializationInfo, StreamingContext)

Inizializza una nuova istanza della classe UnauthorizedAccessException con dati serializzati.

UnauthorizedAccessException(String)

Inizializza una nuova istanza della classe UnauthorizedAccessException con un messaggio di errore specificato.

UnauthorizedAccessException(String, Exception)

Inizializza una nuova istanza della classe UnauthorizedAccessException con un messaggio di errore specificato e un riferimento all'eccezione interna che è la causa dell'eccezione corrente.

Proprietà

Data

Ottiene una raccolta di coppie chiave/valore che forniscono informazioni definite dall'utente aggiuntive sull'eccezione.

(Ereditato da Exception)
HelpLink

Ottiene o imposta un collegamento al file della Guida associato all'eccezione.

(Ereditato da Exception)
HResult

Ottiene o imposta HRESULT, un valore numerico codificato che viene assegnato a un'eccezione specifica.

(Ereditato da Exception)
InnerException

Ottiene l'istanza di Exception che ha causato l'eccezione corrente.

(Ereditato da Exception)
Message

Ottiene un messaggio che descrive l'eccezione corrente.

(Ereditato da Exception)
Source

Ottiene o imposta il nome dell'oggetto o dell'applicazione che ha generato l'errore.

(Ereditato da Exception)
StackTrace

Ottiene una rappresentazione di stringa dei frame immediati nello stack di chiamate.

(Ereditato da Exception)
TargetSite

Ottiene il metodo che genera l'eccezione corrente.

(Ereditato da Exception)

Metodi

Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
GetBaseException()

Quando ne viene eseguito l'override in una classe derivata, restituisce l'Exception che è la causa radice di una o più eccezioni successive.

(Ereditato da Exception)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetObjectData(SerializationInfo, StreamingContext)

Quando ne viene eseguito l'override in una classe derivata, imposta il controllo SerializationInfo con le informazioni sull'eccezione.

(Ereditato da Exception)
GetType()

Ottiene il tipo di runtime dell'istanza corrente.

(Ereditato da Exception)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
ToString()

Crea e restituisce una rappresentazione di stringa dell'eccezione corrente.

(Ereditato da Exception)

Eventi

SerializeObjectState
Obsoleta.

Si verifica quando un'eccezione viene serializzata per creare un oggetto di stato eccezione contenente i dati serializzati relativi all'eccezione.

(Ereditato da Exception)

Si applica a

Vedi anche