错误重命名目录时拒绝访问路径 [英] Error Access to the path is denied when Rename a Directory

查看:406
本文介绍了错误重命名目录时拒绝访问路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重命名一个文件夹,其中新名称在文本框中输入

 txtFilenFolderName 





  protected   void  btnUpdate_Click( object  sender,EventArgs e)
{
string [] values = EditValue;

string oldpath = values [ 0 ]; // =D:\\C#Projects \\website \\Lecturer \\giangvien \\新文件夹

string oldName = values [ 2 ] ; // =新文件夹

string newName = txtFilenFolderName.Text; // =新文件夹1
string newPath = string .Empty;


if (oldName!= newName)
{
newPath = oldpath.Replace(oldName,新名字);
Directory.Move(oldpath,newPath);
}
else
lblmessage2.Text = 新名称不得与旧名称;
}
}





尝试调试:



 oldpath =   D:\\C#Projects \\\ \\website \\Lecturer \\giangvien\\New folder 
oldName = 文件夹
newName = Folder1
newpath = D: \\\\#Projects \\website \\Lecturer \\giangvien \\New folder1





一切似乎都是正确的,但是当我点击按钮编辑--->重命名--->更新--->发生错误:

拒绝访问路径D:\\C#Projects \\website \\Lecturer \\giangvien \\New folder



帮助!!!

解决方案

你要么没有权限重命名文件夹,要么就是文件该文件夹中的某个位置或其子文件夹保持打开状态。如果文件夹中有文件打开,则无法重命名文件夹路径的任何部分。


您的目录移动方法调用应位于try catch块内



 尝试 
{
Directory.Move(oldpath,newPath);
}
catch (UnauthorizedAccessException ex)
{
// 用户无权将目录从源位置移动到目标位置。
}
carch(例外e)
{
// 所有其他例外。
}


I want to rename the a folder with the new name inputed in a Textbox

txtFilenFolderName



protected void btnUpdate_Click(object sender, EventArgs e)
   {
           string[] values = EditValue;

           string oldpath = values[0];// = "D:\\C#Projects\\website\\Lecturer\\giangvien\\New folder"

           string oldName = values[2]; //= New Folder

           string newName = txtFilenFolderName.Text; //= New Folder1
           string newPath = string.Empty;


           if (oldName != newName)
           {
                   newPath = oldpath.Replace(oldName, newName);
                   Directory.Move(oldpath, newPath);
           }
           else
                lblmessage2.Text = "New name must not be the same as the old ";
       }
   }



Try to debug:

oldpath = "D:\\C#Projects\\website\\Lecturer\\giangvien\\New folder"
 oldName = New Folder
newName= New Folder1
newpath = "D:\\C#Projects\\website\\Lecturer\\giangvien\\New folder1"



Everything seems right, but I when I click on buton Edit ---> rename---> Update---> an error occur:
Access to the path is denied D:\\C#Projects\\website\\Lecturer\\giangvien\\New folder

Help!!!

解决方案

You either don't have permissions to rename the folder or there's a file somewhere in that folder or its subfolders that's being held open. You cannot rename any part of a folderpath if there is a file open in it.


Your directory move method call should be inside a try catch block

try
{
  Directory.Move(oldpath, newPath);
}
catch(UnauthorizedAccessException  ex)
{
   //User have no permission to move directory from source to destination location.
}
carch(Exception e)
{
   //all others exception.
}


这篇关于错误重命名目录时拒绝访问路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆