如何重命名这是目前Windows资源管理器打开C#中的文件夹 [英] How to rename a folder in c# which is currently opened by windows explorer

查看:163
本文介绍了如何重命名这是目前Windows资源管理器打开C#中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中重命名的文件夹, System.IO.Directory.Move 抛出 System.IO.IOException (消息拒绝访问),如果该文件夹或子文件夹的任何现由一名(Windows 7)中资源管理器窗口中打开。
使用命令行重命名失败了。
使用第二资源管理器窗口成功。

When renaming a folder in C#, System.IO.Directory.Move throws System.IO.IOException (message "access denied") if that folder or any subfolder is currently opened by a (Windows 7) explorer window. Using the commandline RENAME fails, too. Using a second explorer windows succeeds.

错误甚至崩溃的父文件夹(或其父母)后仍然存在。实际上特定浏览器窗口需要关闭。
这样的探险家,似乎只是创造一些门锁,以显示该文件夹结构,不释放他们,即使不再显示实际的文件夹心不是(这是纯粹的nonsens IMO)。

The error persists even after collapsing the parent folder (or its parents). In fact the particular explorer window needs to be closed. So the explorer seems to create some locks just to show the folder structure and does not release them even if the actual folder isnt displayed anymore (which is pure nonsens IMO).

有没有办法到一个文件夹(使用C#程序如)重命名,即目前由一个浏览器窗口?

Is there a way to rename a folder (in program e.g. using C#), that is presently displayed (or was visible, see above) by an explorer window?

更新

找到一种方式,通过我自己的答案说明了这个问题用 SHFileOperation(见下文)( )。然而,这种解决方案不是非常可行的(见下文)。

Found a way as described by my own answer to this question (see below) using SHFileOperation(). However, this solution is not very feasible (see also below).

推荐答案

所以,我经过进一步回答我的问题

So I'm answering my own question after some further reasearch..

文件夹可以使用 SHFileOperation()如下所示改名HREF =https://msdn.microsoft.com/en-us/library/windows/desktop/bb776887%28v=vs.85%29.aspx相对=nofollow> https://msdn.microsoft.com /en-us/library/windows/desktop/bb776887%28v=vs.85%29.aspx
(这是否使用由沃特或没有提及魔力 - 。)

The folder can be renamed using SHFileOperation() as shown here: https://msdn.microsoft.com/en-us/library/windows/desktop/bb776887%28v=vs.85%29.aspx (whether this uses the 'magic' mentioned by Wouter or not. ;-)

但如果是windows / .NET API,如 System.IO.Directory.Move ,跆拳道,我需要使用Shell?不是说性能...

But if there is a windows/.Net API such as System.IO.Directory.Move, WTF do I need to use the Shell? Not talking about performance ...

总之,使用 SHFileOperation()是一个痛苦..使用C#,因为你需要声明这一切对调用的东西。而在你需要使用不同的结构为32位和64位的Windows,因为包装不同的是这种特殊情况下(见的 http://www.pinvoke.net/default.aspx/shell32.shfileoperation )。这是很麻烦的,因为通常我会指定的值为anycpu 的作为靶。在这一点上,你要么需要在运行时(非常糟糕确实)分支,这取决于你是否是64位或32位的过程,或者你打造不同的两个不同的目标,这是一个相当大的影响,只是为了解决无聊的探险家。

Anyway, using SHFileOperation() is a pain in the a.. using C# since you need to declare all this p-invoke stuff. And in this particular case you need to use different structs for 32 and 64-bit windows, since the packing is different (see http://www.pinvoke.net/default.aspx/shell32.shfileoperation). This is very cumbersome, as usually I'd specify AnyCPU as target. At this point you either need to branch at runtime (very bad indeed), depending whether you are a 64 or 32 bit process, or you build differently for two different targets, which is quite a big impact just to work around the silly explorer.

问候

这篇关于如何重命名这是目前Windows资源管理器打开C#中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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