• c#怎么解决System.UnauthorizedAccessException异常


    https://blog.csdn.net/qq_38061677/article/details/81157116

    代码:

    using System;
    namespace Project2048
    {
    class Program
    {
    static void Main(string[] args)
    {
    FileInfo fileInfo1 = new FileInfo("555.txt");//该文件是存在的
    string str = "hello";
    File.AppendAllText(fileInfo1.DirectoryName, str);
    Console.ReadKey();
    }

    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    System.UnauthorizedAccessException解析
    在VS默认的解释是: path 指定了一个只读文件。- 或 -在当前平台上不支持此操作。- 或 -path 指定了一个目录。- 或 -调用方没有所要求的权限。
    疑惑

    FIleInfo类实例化,默认的的只读属性是false,也就是说。可以进行读写的,怎么会出System.UnauthorizedAccessException”错误?

    经过仔细寻找发现问题是fileInfo1.DirectoryName,这个方法是获取文件的目录,并不是到当前文件。使用fileInfo.Name就可以了
    代码:

    using System;
    namespace Project2048
    {
    class Program
    {
    static void Main(string[] args)
    {
    FileInfo fileInfo1 = new FileInfo("555.txt");//该文件是存在的
    string str = "hello";
    File.AppendAllText(fileInfo1.Name, str);
    Console.ReadKey();
    }

    }
    }
    ---------------------
    作者:小小liang
    来源:CSDN
    原文:https://blog.csdn.net/qq_38061677/article/details/81157116
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    springboot整合log4j2
    document解析html
    关于字符串读写乱码的解决方法浅谈
    BOM知识整理
    es6的Map()构造函数
    es6的Set()构造函数
    关于node的path模块
    java总结(方法与对象)
    webpack3.0+总结
    java总结(二)(运算符)
  • 原文地址:https://www.cnblogs.com/zkwarrior/p/11151972.html
Copyright © 2020-2023  润新知