【问题标题】:UnauthorizedAccessException even though everything could be accessed? [duplicate]UnauthorizedAccessException 即使可以访问所有内容? [复制]
【发布时间】:2020-01-27 17:26:30
【问题描述】:

所以即使我们从防病毒/防火墙中清除了该应用程序,我的程序也会一直显示 UnauthorizedAccessException。错误发生在这一行:

FileStream log = File.Create(path);

它说我无法访问该路径。下面是上下文中的代码。

  private void StartConversion1(object sender, RoutedEventArgs e)
    {


        String path = Properties.Settings.Default.label;
        FileStream log = File.Create(path);
        StreamWriter sw = new StreamWriter(log);
        foreach (String f in filesToProcess1)
        {
            // rest of the code

谢谢

【问题讨论】:

标签: c# visual-studio exception


【解决方案1】:

从错误中可以看出这是一个权限问题

  • 检查您的应用是否有足够的权限写入上述目录
  • 删除与目录关联的只读属性

    var di = new DirectoryInfo("Path to directory");
    di.Attributes &= ~FileAttributes.ReadOnly;
    

结论:

    if(Directory.Exists(inputdirpath)) 
    {
    //Code to remove read-only attribute

    //Code to create File
    }

【讨论】:

    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    相关资源
    最近更新 更多