允许 IFileOpenDialog 选取尚未创建的文件

Allow IFileOpenDialog to pick a file that is not created yet

本文关键字:创建 文件 IFileOpenDialog 选取 允许      更新时间:2023-10-16

我在许多应用程序中都看到过这种情况。您可以选择打开文件,如果它不存在,则会创建该文件,并且您不会收到任何投诉。所有这些都来自同一个"打开文件">对话框。我使用 IFileOpenDialog 打开一个文件,如果我输入一个不存在的文件,它会显示一个错误,我无法获取此文件的路径。

我想要的是不要收到错误,而是接受不存在的文件名。稍后我将创建它。这可能吗?

if (SUCCEEDED(hr))
{
    IFileOpenDialog *pFileOpen;
    // Create the FileOpenDialog object
    hr = CoCreateInstance(
        CLSID_FileOpenDialog,
        NULL, 
        CLSCTX_ALL, 
        IID_IFileOpenDialog, 
        reinterpret_cast<void**>(&pFileOpen)
    );
    if (SUCCEEDED(hr))
    {
        // Show the Open dialog box.
        hr = pFileOpen->Show(NULL);
        // Get the file name from the dialog box.
        if (SUCCEEDED(hr))
        {
            IShellItem *pItem;
            hr = pFileOpen->GetResult(&pItem);
            if (SUCCEEDED(hr))
            {
                PWSTR pszFilePath;
                hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
                // Check if file actually exists
                if (SUCCEEDED(hr))
                {
                    // Create file if not found
                    if (PathFileExists(pszFilePath) != 1)
                    {
                    }
                    CoTaskMemFree(pszFilePath);
                }
                pItem->Release();
            }
        }
        pFileOpen->Release();
    }
    CoUninitialize();
}
call IFileDialog::GetOptions获取

默认选项,删除FOS_PATHMUSTEXIST位和FOS_FILEMUSTEXIST位,然后使用IFileDialog::SetOptions重新设置新选项。