Why is access denied when I try to move a directory?

I have two directories: folder1 and folder2. folder1 contains a file. I'd like to move folder1 under folder2 to result in folder2folder1. When I try to do this with the C# code below, I get:

System.IO.IOException: Access to the path 'E:wwwdevtestMoveDirectoriesfolder1' is denied.

The relevant code:

// In Page_Load.
MoveDirectory("folder1");

// Method for moving directories.
protected void MoveDirectory(string strMoveThis)
{
    try
    {
        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Server.MapPath(strMoveThis));
        dir.MoveTo(Server.MapPath("folder2"));
    }
    catch (Exception ex)
    {
        Response.Write(ex);
    }
}

My ASP.NET 4.0 app pool has modify privileges on the folder1. This is actually a test application with code that has been pulled from a much bigger application, so it doesn't have all of the testing and exception handling one would expect.

EDIT: I found that I can create files within folder1.


I hate to answer my own question, but...

Basically, I updated this:

dir.MoveTo(Server.MapPath("folder2"));

to this:

dir.MoveTo(Server.MapPath("folder2" + strMoveThis));

Same permissions, but a better formation of the path. Thanks for your help, everyone!

链接地址: http://www.djcxy.com/p/36090.html

上一篇: 将名称而不是指定文件夹中所有文件夹的路径存储到列表中?

下一篇: 为什么当我尝试移动目录时访问被拒绝?