zoukankan      html  css  js  c++  java
  • 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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    [Kali_Debian] 清除无用的库文件(清理系统,洁癖专用)-布布扣-bubuko.com
    给 Linux 系统“减肥”,系统垃圾清理_系统安装与配置管理_Linux Today
    命令行选项
    SQL 优化
    精通initramfs构建step by step
    常用正则表达式
    Chrome_浏览器开发人员工具
    按键精灵
    CMD命令大全
    50种折纸方法
  • 原文地址:https://www.cnblogs.com/zkwarrior/p/11151972.html
Copyright © 2011-2022 走看看