C# - 重命名目录的方法 [英] C# - Ways to rename a directory

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

问题描述

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



所以我的问题是:是否有其他方法要重命名目录而不使用 Directory.Move



我以为使用命令shell( Process.Start()),但这应该是我最后一次这样做。






<更多细节



程序仍在运行,我得到异常,可以在Windows资源管理器中手动重命名(右键单击 - >重命名),而我的光标在catch块的断点上暂停。我还尝试在 Directory.Move 中设置断点,成功地将资源管理器中的目录重命名(再次返回),逐步执行 Directory.Move 并且最后在$ code> catch(IOException)再次。



这是我的代码

  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)
{
//断点
}
}
}

正如您所看到的,我刚刚输入了该方法。



由于这种方式对我来说无法工作,我需要找到另一种方法。






解决方案

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

SHFILEOPSTRUCT struc = new SHFILEOPSTRUCT();

struc.hNameMappings = IntPtr.Zero;
struc.hwnd = IntPtr.Zero;
struc.lpszProgressTitle =重命名发行目录;
struc.pFrom = destPathRelease +'\0';
struc.pTo = ThisUser.DestPath +'\\'+(string.IsNullOrEmpty(this.currBuildLabel)?(Release+'_'+ DateTime.Now.ToString(yyyyMMdd_HHmmss)): this.currBranchName)+'。'+ this.currBuildLabel +'\0';
struc.wFunc = FileFuncFlags.FO_RENAME;

int ret = SHFileOperation(ref struc);
}

请注意使用 pTo pFrom 作为零分隔的双零终止字符串。

解决方案

p>您可以尝试使用P / Invoke调用 SHFileOperation API(或 IFileOperation 界面)。这是Explorer在理论上使用的,所以它应该更直接地复制功能。


I'm using Directory.Move(oldDir, newDir) to rename a directory. Every now and then I get an 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.

So my question is: Are there any other ways to rename a directory without using Directory.Move?

I thought of using the command shell (Process.Start()) but this should be my last way of doing it.


Further details

The program is still running, I get the exception and can rename it manually in windows explorer (right click -> rename) while my cursor is paused on the breakpoint in the catch block. 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.

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.

Since this way is not working for me I need to find another way to do so.


Solution

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

    SHFILEOPSTRUCT struc = new SHFILEOPSTRUCT();

    struc.hNameMappings = IntPtr.Zero;
    struc.hwnd = IntPtr.Zero;
    struc.lpszProgressTitle = "Rename Release directory";
    struc.pFrom = destPathRelease + '\0';
    struc.pTo = ThisUser.DestPath + '\\' + (string.IsNullOrEmpty(this.currBuildLabel) ? ("Release" + '_' + DateTime.Now.ToString("yyyyMMdd_HHmmss")) : this.currBranchName) + '.' + this.currBuildLabel + '\0';
    struc.wFunc = FileFuncFlags.FO_RENAME;

    int ret = SHFileOperation(ref struc);
}

Please note it's important to use pTo and pFrom as zero delimited double zero terminated strings.

解决方案

You could try using P/Invoke to call the SHFileOperation API (or the IFileOperation interface). This is what Explorer uses in theory, so it should replicate the functionality more directly.

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

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