Access To The Path … Is Denied

隐身守侯 提交于 2019-12-01 21:26:33

While it is an Access Denied exception, it sounds like the text files are in use and cannot be moved because there are open references to the file.

The File.Create method returns a FileStream object which I'd imagine must be closed/disposed before the files can be modified.

Try the following for your inner loop:

  for (int j = 0; j < 5; j++)
  {
    using(var fs = File.Create(Dir1.FullName + "\\Sub"+(i+1).ToString() + "\\text"+(j+1).ToString() + ".txt"))
    {
        //fs.WriteByte(...);
        fs.Close();
    }
  }

First off, you should use Path.Combine instead of doing string concatenation.
Second off, the stack trace isn't as helpful as the exception being thrown.

I imagine your problem might be fixed by doing this though:

Directory.Move(Dir1.FullName, Dir2.FullName);

If that fixes it, then the issue is with the DIR1 subdirectory you're trying to move it to.

As a debugging, step you should set failure auditing on the two folders (under advanced security settings). Just set everyone to audit all failures, then try your operation again. Depending on the OS version that you are running you should get the user account being used for the operation and what privilege was missing. Also make sure that there are no deny permissions set on the folder as they override all other permissions. You will want to look at the security event log. If there are no failure audits for the operation then it is not a permissions issues.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!