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
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    bzoj1653 [Usaco2006 Feb]Backward Digit Sums
    python爬虫之真实世界中的网页解析
    python爬虫之网页解析
    python列表操作方法
    python多行代码简化
    python操作文件
    python数据类型之pandas—DataFrame
    python数据类型之字典(二)
    python数据类型之字典(一)
    Selenium模拟浏览器抓取淘宝美食信息
  • 原文地址:https://www.cnblogs.com/zkwarrior/p/11151972.html
Copyright © 2011-2022 走看看