Resolved Error - File/Folder is being used by another process
As i discussed in my current project i made a self explorer.exe so i accessed all the folders,files .i am doing same behaviour as lioke window explorer.exe. But I was facing a error This file/folder is being used by another process and all that and i face all these errors when i rename,delete,move,copy,drag-drop.
Solution :- I add folders runtime and updation is all as real time.so I used shell com object and make tree using shell namespace like :-
node is treeNode of tree.
Shell32.FolderItem folderItem = (Shell32.FolderItem)node.Tag.FolderItem;
Shell32.Folder folder = (Shell32.Folder)folderItem.GetFolder;
then iterate thorugh foreach and used break statement because i add a dummynode in each node who have folders and when i expand that node then delete the dummynode and create the nodes for that treenode beacuase of efficiency.So on creating dummyNode i used foreach statement like :-
bool hasFolders = false;
foreach (Shell32.FolderItem item in folder.Items())
{
if (item.IsFileSystem && item.IsFolder && !item.IsBrowsable)
{
hasFolders = true;
break;
}
}
if (hasFolders)
{
TreeNode newTreeNode = new TreeNode();
newTreeNode.Tag = STRING_DUMMY_TREENODE;
treeNode.Nodes.Add(newTreeNode);
}
so this foreach actually its an iterator so when i use break statement so tis break from the loop but still it holds the object and shell thinks its used by some other program and all so dont use foreach if you using break statement like this :-
Shell32.FolderItems items = folder.Items();
for (int itemIndex = 0; itemIndex < items.Count; itemIndex++)
{
Shell32.FolderItem item = items.Item(itemIndex);
if (item.IsFileSystem && item.IsFolder && !item.IsBrowsable)
{
hasFolders = true;
break;
}
}
and now as above you will never face the error like file/folder is being used by another process so in your application if you are facing then firstly check it out and make sure you are not using any iterator.
Happy to code.
0 comments:
Post a Comment