允许 IFileOpenDialog 选择一个尚未创建的文件

Allow IFileOpenDialog to pick a file that is not created yet

我在很多应用程序中都看到过这种情况。您可以选择打开文件,如果文件不存在,则会创建文件,您不会收到任何投诉。全部来自同一个 Open File 对话框。 我使用 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 设置新选项。