本文整理汇总了C#中System.UnauthorizedAccessException类的典型用法代码示例。如果您正苦于以下问题:C# UnauthorizedAccessException类的具体用法?C# UnauthorizedAccessException怎么用?C# UnauthorizedAccessException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UnauthorizedAccessException类属于System命名空间,在下文中一共展示了UnauthorizedAccessException类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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();
}
}
}
输出:
UnAuthorizedAccessException: Unable to access file. The file is read-only.