UnauthorizedAccessException Clase
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Excepción que se produce cuando el sistema operativo deniega el acceso debido a un error de E/S o a un tipo específico de error de seguridad.
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
- Herencia
- Herencia
- Derivado
- Atributos
En el ejemplo siguiente se muestra la excepción UnauthorizedAccessException que se produce al intentar escribir en un archivo de solo lectura.
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.
Normalmente, una excepción de UnauthorizedAccessException se produce mediante un método que encapsula una llamada a la API de Windows. Para encontrar los motivos de la excepción, examine el texto de la propiedad Message del objeto de excepción.
UnauthorizedAccessException usa el HRESULT
COR_E_UNAUTHORIZEDACCESS
, que tiene el valor 0x80070005.
Unauthorized |
Inicializa una nueva instancia de la clase UnauthorizedAccessException. |
Unauthorized |
Obsoletos.
Inicializa una nueva instancia de la clase UnauthorizedAccessException con datos serializados. |
Unauthorized |
Inicializa una nueva instancia de la clase UnauthorizedAccessException con un mensaje de error especificado. |
Unauthorized |
Inicializa una nueva instancia de la clase UnauthorizedAccessException con un mensaje de error especificado y una referencia a la excepción interna que es la causa de esta excepción. |
Data |
Obtiene una colección de pares clave-valor que proporcionan información adicional definida por el usuario sobre la excepción. (Heredado de Exception) |
Help |
Obtiene o establece un vínculo al archivo de ayuda asociado a esta excepción. (Heredado de Exception) |
HResult |
Obtiene o establece HRESULT, un valor numérico codificado que se asigna a una excepción específica. (Heredado de Exception) |
Inner |
Obtiene la instancia de Exception que provocó la excepción actual. (Heredado de Exception) |
Message |
Obtiene un mensaje que describe la excepción actual. (Heredado de Exception) |
Source |
Obtiene o establece el nombre de la aplicación o el objeto que provoca el error. (Heredado de Exception) |
Stack |
Obtiene una representación de cadena de los fotogramas inmediatos en la pila de llamadas. (Heredado de Exception) |
Target |
Obtiene el método que produce la excepción actual. (Heredado de Exception) |
Equals(Object) |
Determina si el objeto especificado es igual al objeto actual. (Heredado de Object) |
Get |
Cuando se reemplaza en una clase derivada, devuelve el Exception que es la causa principal de una o varias excepciones posteriores. (Heredado de Exception) |
Get |
Actúa como función hash predeterminada. (Heredado de Object) |
Get |
Obsoletos.
Cuando se reemplaza en una clase derivada, establece el SerializationInfo con información sobre la excepción. (Heredado de Exception) |
Get |
Obtiene el tipo de tiempo de ejecución de la instancia actual. (Heredado de Exception) |
Memberwise |
Crea una copia superficial del Objectactual. (Heredado de Object) |
To |
Crea y devuelve una representación de cadena de la excepción actual. (Heredado de Exception) |
Serialize |
Obsoletos.
Se produce cuando se serializa una excepción para crear un objeto de estado de excepción que contiene datos serializados sobre la excepción. (Heredado de Exception) |
Produto | Versións |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
Comentarios de .NET
.NET é un proxecto de código aberto. Selecciona unha ligazón para ofrecer comentarios: