In UWP, can I check if a StorageFolder is a parent of another StorageFolder?

huangapple go评论60阅读模式
英文:

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");    
}

huangapple
  • 本文由 发表于 2023年4月4日 18:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定