UnauthorizedAccessException即使所有内容都可以访问?

时间:2020-01-27 17:26:30

标签: c# visual-studio exception

因此,即使我们从防病毒/防火墙清除了该应用程序,我的程序仍会说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

谢谢

1 个答案:

答案 0 :(得分:0)

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

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

    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
    }