我使用Windows API代碼包中的CommonOpenFileDialog作爲文件夾選取器對話框。我將InitialDirectory屬性設置爲Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)。但是,當我顯示對話框時,地址欄中的路徑是Libraries \ Documents(不是我所期望的C:\ users \ craig \ my文檔)。另外,如果我只按下「選擇文件夾」按鈕,我會看到一個對話框,說'你選擇了一個圖書館。請選擇一個文件夾。'如何獲取CommonOpenFileDialog的InitialDirectory作爲用戶的MyDocuments路徑而不是Libraries Documents?
有人知道爲什麼我的文件路徑被忽略,有利於'庫\文件'?更重要的是,如何讓對話框遵守我傳入的InitialDirectory值?
我使用該對話框中的代碼是:
if (CommonFileDialog.IsPlatformSupported)
{
var folderSelectorDialog = new CommonOpenFileDialog();
folderSelectorDialog.EnsureReadOnly = true;
folderSelectorDialog.IsFolderPicker = true;
folderSelectorDialog.AllowNonFileSystemItems = false;
folderSelectorDialog.Multiselect = false;
folderSelectorDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
folderSelectorDialog.Title = "Project Location";
if (folderSelectorDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
ShellContainer shellContainer = null;
try
{
// Try to get a valid selected item
shellContainer = folderSelectorDialog.FileAsShellObject as ShellContainer;
}
catch
{
MessageBox.Show("Could not create a ShellObject from the selected item");
}
FilePath = shellContainer != null ? shellContainer.ParsingName : string.Empty;
}
}
感謝,
-Craig
你從 「Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)」 得到什麼路徑? – 2011-03-24 21:41:53
庫/文檔*是*您Win7上的我的文檔文件夾。有用。 – 2011-03-24 23:19:19
@Brent:你得到的路徑是C:\ users \ Craig \ My Documents,假設Craig是你的用戶名 – Craig 2011-03-25 15:33:24