Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:
    {
        time_t nowTime;
        time(&nowTime);

        tm nowTm;
        localtime_s(&nowTm, &nowTime);

        wchar_t buf[64];
        wcsftime(&buf[0], ARRAYSIZE(buf), L"%F %T", &nowTm);

        const auto defaultFileName = RS_(L"TerminalChatHistoryDefaultFileName") + winrt::to_hstring(buf);
        _SaveFileHelper(text, L"", defaultFileName);
    }

    fire_and_forget TerminalPage::_SaveFileHelper(const winrt::hstring& text, const winrt::hstring& filepath, const std::wstring_view filename)
    {
        // This will be used to set up the file picker "filter", to select .txt
        // files by default.
        static constexpr COMDLG_FILTERSPEC supportedFileTypes[] = {
            { L"Text Files (*.txt)", L"*.txt" },
            { L"All Files (*.*)", L"*.*" }
        };
        // An arbitrary GUID to associate with all instances of this
        // dialog, so they all re-open in the same path as they were
        // open before:
        static constexpr winrt::guid clientGuidExportFile{ 0xF6AF20BB, 0x0800, 0x48E6, { 0xB0, 0x17, 0xA1, 0x4C, 0xD8, 0x73, 0xDD, 0x58 } };

        try
        {
            auto path = filepath;

            if (path.empty())
            {
                // GH#11356 - we can't use the UWP apis for writing the file,
                // because they don't work elevated (shocker) So just use the
                // shell32 file picker manually.
                std::wstring cleanedFilename{ til::clean_filename(std::wstring{ filename }) };
                path = co_await SaveFilePicker(*_hostingHwnd, [filename = std::move(cleanedFilename)](auto&& dialog) {
                    THROW_IF_FAILED(dialog->SetClientGuid(clientGuidExportFile));
                    try
                    {
                        // Default to the Downloads folder
                        auto folderShellItem{ winrt::capture<IShellItem>(&SHGetKnownFolderItem, FOLDERID_Downloads, KF_FLAG_DEFAULT, nullptr) };
                        dialog->SetDefaultFolder(folderShellItem.get());
                    }
                    CATCH_LOG(); // non-fatal
                    THROW_IF_FAILED(dialog->SetFileTypes(ARRAYSIZE(supportedFileTypes), supportedFileTypes));
                    THROW_IF_FAILED(dialog->SetFileTypeIndex(1)); // the array is 1-indexed
                    THROW_IF_FAILED(dialog->SetDefaultExtension(L"txt"));

                    // Default to using the tab title as the file name

Review comment (suggestion):
If you use the same GUID for every Save File operation, it will remember the saving folder regardless of what you are saving.

Put another way: the GUID disambiguates _types of things being saved_, in case the user wants to save all Terminal Buffers to one place and all Chat Histories to another place.

Code after:
    {
        time_t nowTime;
        time(&nowTime);

        tm nowTm;
        localtime_s(&nowTm, &nowTime);

        wchar_t buf[64];
        wcsftime(&buf[0], ARRAYSIZE(buf), L"%F %T", &nowTm);

        const auto defaultFileName = RS_(L"TerminalChatHistoryDefaultFileName") + winrt::to_hstring(buf);

        // An arbitrary GUID to associate with all instances of the save file dialog
        // for exporting terminal chat histories, so they all re-open in the same path as they were
        // open before:
        static constexpr winrt::guid terminalChatSaveFileDialogGuid{ 0xc3e449f6, 0x1b5, 0x44e0, { 0x9e, 0x6d, 0x63, 0xca, 0x15, 0x43, 0x4b, 0xdc } };

        _SaveStringToFileOrPromptUser(text, L"", defaultFileName, terminalChatSaveFileDialogGuid);
    }

    // Method Description:
    // - Saves the given text to the file path provided, or prompts the user for the location to save it
    // Arguments:
    // - text - the text to save
    // - filepath - the location to save the text
    // - filename - the name of the file to save the text to
    // - dialogGuid - the guid to associate with these specific saves (determines where the save dialog opens to by default)
    fire_and_forget TerminalPage::_SaveStringToFileOrPromptUser(const winrt::hstring& text, const winrt::hstring& filepath, const std::wstring_view filename, const winrt::guid dialogGuid)
    {
        // This will be used to set up the file picker "filter", to select .txt
        // files by default.
        static constexpr COMDLG_FILTERSPEC supportedFileTypes[] = {
            { L"Text Files (*.txt)", L"*.txt" },
            { L"All Files (*.*)", L"*.*" }
        };

        try
        {
            auto path = filepath;

            if (path.empty())
            {
                // GH#11356 - we can't use the UWP apis for writing the file,
                // because they don't work elevated (shocker) So just use the
                // shell32 file picker manually.
                std::wstring cleanedFilename{ til::clean_filename(std::wstring{ filename }) };
                path = co_await SaveFilePicker(*_hostingHwnd, [filename = std::move(cleanedFilename), saveDialogGuid = std::move(dialogGuid)](auto&& dialog) {
                    THROW_IF_FAILED(dialog->SetClientGuid(saveDialogGuid));
                    try
                    {
                        // Default to the Downloads folder