无法重命名目录在C#但手动 [英] Can't rename directory in C# but manually

查看:209
本文介绍了无法重命名目录在C#但手动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Directory.Move(oldDir,newDir)重命名目录。现在,我得到一个 IOException 访问路径oldDir被拒绝。但是,如果我右键单击资源管理器中的目录,我可以重命名它没有任何问题。如何才能让它工作?



编辑



程序仍在运行,我得到异常,并可以在我的光标在断点上暂停时手动重命名。我还尝试在 Directory.Move 中设置断点,成功地将资源管理器中的目录重命名(再次返回),逐步执行 Directory.Move 并最终在$ code> catch(IOException)再次。所以我不明白为什么我的程序应该锁定目录。必须有其他的东西。



任何想法?



编辑2 p>

这是我的代码

  public bool Copy()
{
string destPathRelease = ThisUser.DestPath +\\Release;

if(Directory.Exists(destPathRelease))
{
try
{
string newPath = ThisUser.DestPath +'\\'+ (string.IsNullOrEmpty(currBuildLabel)?(Release+'_'+ DateTime.Now.ToString(yyyyMMdd_HHmmss)):currBranchName)+'。'+ currBuildLabel;
Directory.Move(destPathRelease,newPath);

catch(IOException)
{
//断点
}
}
}
}

正如你所看到的,我刚刚输入了该方法。我从来没有碰过我的程序中的目录。有没有另一种方式来重命名目录?

解决方案

没有看到更多的代码,我会说你的应用程序是锁定一个文件目录,您可以使用进程资源管理器查看访问目录的内容。 p>

从介绍到过程浏览器:



有没有想过哪个程序有特定的文件或目录打开?现在你可以找出来Process Explorer显示有关哪些处理程序和DLL进程已打开或加载的信息。



也许值得一提的是,从/那个目录 - 例如Dropbox的。我最近有一个问题,视觉工作室将因为一个文件锁停止调试 - 最终是在暂时锁定文件的驱动器上进行索引。过程浏览器仅部分帮助它显示系统具有文件锁定,而不是另一个应用程序。


I'm using Directory.Move(oldDir, newDir) to rename a directory. Every now and then I get a IOException saying "Access to the path "oldDir" is denied". However if I right click the directory in the explorer I can rename it without any issues. How's that and how can I get it to work?

EDIT

The program is still running, I get the exception and can rename it manually while my cursor is paused on the breakpoint. I also tried setting a breakpoint at Directory.Move, successfully renamed the directory in explorer (and back again), stepped over Directory.Move and ended up in the catch (IOException) again. So I don't see why my program should lock the directory at all. There must be something else.

Any ideas?

EDIT 2

Here is my code

public bool Copy()
{
    string destPathRelease = ThisUser.DestPath + "\\Release";

    if (Directory.Exists(destPathRelease))
    {
        try
        {
            string newPath = ThisUser.DestPath + '\\' + (string.IsNullOrEmpty(currBuildLabel) ? ("Release" + '_' + DateTime.Now.ToString("yyyyMMdd_HHmmss")) : currBranchName) + '.' + currBuildLabel;
            Directory.Move(destPathRelease, newPath);

            catch (IOException)
            {
               // Breakpoint
            }
        }
    }
}

As you can see I just entered the method. I never touched the directory in my program before. Is there another way to rename a directory?

解决方案

Without seeing more code I'd say your application is locking a file within the directory, you can see what is accessing the directory using Process explorer

from the intro to process explorer:

Ever wondered which program has a particular file or directory open? Now you can find out. Process Explorer shows you information about which handles and DLLs processes have opened or loaded.

It might also be worth making sure nothing else is copying files from/to that directory - e.g. dropbox. I had an issue recently where visual studio would stop debugging because of a file lock - in the end it was indexing on the drive which was temporarily locking the file. Process explorer only partially helped in that it showed 'system' had the file lock and not another application.

这篇关于无法重命名目录在C#但手动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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