Directory.Move功能在服务器中不起作用

时间:2013-05-23 11:13:14

标签: c# asp.net .net

我想将文件夹移动到服务器中的其他文件夹。在本地,我的代码成功运行。但是在现场主持人中,它不起作用。它与权限有关吗?

代码示例

string from = Server.MapPath(MainRoot + values[1].ToString());
string to = Server.MapPath(MainRoot + newFolderPath);
Directory.Move(from, to); 

1 个答案:

答案 0 :(得分:2)

如果您无法在服务器中调试它,只需尝试在代码中添加一些验证来检查发生了什么。做这样的事情:

try
   {
     string from = Server.MapPath(MainRoot + values[1].ToString());
     string to = Server.MapPath(MainRoot + newFolderPath);

     if(!Directory.Exists(from) || !Directory.Exists(to))
     {
       Throw new Exception("One of the directories doesn't exist");
     }

     Directory.Move(from, to); 
   }  
   Catch(Exception ex)
   {
     File.WriteAllText("Error.txt", ex.Message);
   }

执行后检查Error.txt以查看发生了什么。它会引发异常 如果其中一个目录不存在,那么如果IO也会引发异常 对于不知情者不能进行操作。只需查看日志。

修改

现在您已找到异常,请在运行时创建目录:

 if(!Directory.Exists(from))
 {
    Directory.Create(from);
 }

 if(!Directory.Exists(to))
 {
     Directory.Create(to);
 }