如何获取目标文件夹

How to Get the Target Folder

我刚刚在 Open directory dialog,他们说 "get this package, and do this and this to get a folder select window to show up"。好吧,使用 Windows API 代码包-Shell 包,一切都很好。但是,现在我想获取所选的实际文件夹。我没有注意到他们在任何地方提到这个。

我试过string folderLocation = Convert.ToString(dialog);(dialog是打开文件夹的变量window),但那只给我喜欢属性的变量。我也试过这个:CommonFileDialogResult result = dialog.ShowDialog(); string folderLocation = Convert.ToString(result);

但这只是给了我 "Ok" - 我认为这是它的结果,而不是实际的文件夹。

ShowDialog 的结果仅指示用户是单击确定、取消还是关闭 window。 CommonOpenFileDialog既可以用于文件,也可以用于文件夹,所以用作文件夹选择器时有点奇怪,但是路径存储在FileName.

var dlg = new CommonOpenFileDialog();
dlg.IsFolderPicker = true;
if(dlg.ShowDialog() == CommonFileDialogResult.Ok) {
    Console.WriteLine(dlg.FileName);
}

如果我没理解错的话,您想获取所选文件的文件夹吗?如果是这种情况,您可以获取该文件的 FileInfo,并从中提取 folfer。像这样:

System.IO.FileInfo fInfo = new System.IO.FileInfo(oFD1.FileName);
MessageBox.Show(fInfo.DirectoryName);

PS。 oFD1 是 OpenFileDialog