英文:
In UWP, can I check if a StorageFolder is a parent of another StorageFolder?
问题
I have an app that recursively manage files and folders in a StorageFolder
picked by users (using FolderPicker
). Now users may add multiple folders into the managing list. However right now my app does not know if user may pick a parent folder of another picked one which would cause duplicate files. For example they can pick D:\test\subfolder
and then pick D:\test
(or vice versa).
My question is, giving two StorageFolder
instances, can I request an operation that detects if an instance is a subfolder of the other? I cannot see any obvious method in the API documentation page.
After typing the question, I thought of a workaround that is listing the contents of each folder and see if any child matches the other. I am not even sure if I can do this and if possible I would prefer not doing this because a folder content maybe very large (possibly the entire partition)
英文:
I have an app that recursively manage files and folders in a StorageFolder
picked by users (using FolderPicker
). Now users may add multiple folders into the managing list. However right now my app does not know if user may pick a parent folder of another picked one which would cause duplicate files. For example they can pick D:\test\subfolder
and then pick D:\test
(or vice versa).
My question is, giving two StorageFolder
instances, can I request an operation that detects if an instance is a subfolder of the other? I cannot see any obvious method in the API documentation page.
After typing the question, I thought of a workaround that is listing the contents of each folder and see if any child matches the other. I am not even sure if I can do this and if possible I would prefer not doing this because a folder content maybe very large (possibly the entire partition)
答案1
得分: 1
I think you will be able to use StorageFolder.Path, to get the path of the two instances and then based on that you can determine if they are sub-routed, example:
var file1 = @"D:\test\";
var file2 = @"D:\test\subfolder";
if (file1.StartsWith(file2) || file2.StartsWith(file1)) {
Console.WriteLine("Folders are subrouted");
}
英文:
I think you will be able to use StorageFolder.Path, to get the path of the two instances and then based on that you can determine if they are sub-routed, example:
var file1 = @"D:\test\";
var file2 = @"D:\test\subfolder";
if(file1.StartsWith(file2) || file2.StartsWith(file1)){
Console.WriteLine("Folders are subrouted");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论