Repository: microsoft/terminal (language: cpp)

Issue:
After updating Windows Terminal, pressing Ctrl+Profile will not inherit the working directory
### Windows Terminal version

1.16.10262.0

### Windows build number

10.0.22621.1537

### Other Software

_No response_

### Steps to reproduce

1. Set the startup directory in Windows Terminal settings to ".".
2. Go to a folder in File Explorer (for example: `D:\SomeDir`).
3. Right-click and select "Open with Terminal" from the context menu.
4. Hold down the Ctrl key on the title bar of the new Windows Terminal and create any tab (to create as an administrator).`

### Expected Behavior

In the new Windows Terminal that runs as an administrator, the working directory is inherited as `D:\SomeDir`

### Actual Behavior

In the new Windows Terminal that runs as an administrator, the working directory is reset to `C:\Windows\System32`


Discussion / hints:
In the first stable version that supports creating an administrator profile window by holding down Ctrl (Version `1.15.2525.0`), its behavior was as expected. After Windows Store updated Windows Terminal recently, I found that its behavior was broken.  
Currently I can only solve this behavior change by using the `powershell -Command Start-Process wt  -Verb runAs` command, but it is very inconvenient.
I just tested every stable version and found that `1.15.3465.0` and `1.15.3466.0` are the last stable versions that can inherit the working directory, while `1.16.10261.0` and `1.16.10262.0` are the first stable versions that will not inherit the working directory.
@zadjii-msft hm. `ShellExecute("appsfolder:...")` fallout?
> @zadjii-msft hm. `ShellExecute("appsfolder:...")` fallout?

probably
Also noticed this when looking into #14817 and in my case I was not able to set the working directory for `ShellExecute`. Somehow it was never talking into account and googling around didn't return anything useful.  

`1.15.2525.0` is just doing `CreateProcessW` here https://github.com/microsoft/terminal/blob/release-1.15/src/cascadia/ShellExtension/OpenTerminalHere.cpp#L47 but was not starting elevated then.

So indeed moving to `ShellExecute` is causing this. But don't know what would the best way to set the working directory then tbh.
Hmm. I bet this especially breaks in 1.18. I wonder actually if `"startingDirectory": "."` will be really broken, because the Terminal's CWD will always be the CWD of the _first_ terminal window. 

Butts. 

Fix patch:
diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp
index 2af6a23f2a2..265e2da1eae 100644
--- a/src/cascadia/TerminalApp/TerminalPage.cpp
+++ b/src/cascadia/TerminalApp/TerminalPage.cpp
@@ -1120,6 +1120,7 @@ namespace winrt::TerminalApp::implementation
                 if (profile)
                 {
                     newTerminalArgs.Profile(::Microsoft::Console::Utils::GuidToString(profile.Guid()));
+                    newTerminalArgs.StartingDirectory(_evaluatePathForCwd(profile.EvaluatedStartingDirectory()));
                 }
             }
 
@@ -1165,6 +1166,11 @@ namespace winrt::TerminalApp::implementation
         }
     }
 
+    std::wstring TerminalPage::_evaluatePathForCwd(const std::wstring_view path)
+    {
+        return Utils::EvaluateStartingDirectory(_WindowProperties.VirtualWorkingDirectory(), path);
+    }
+
     // Method Description:
     // - Creates a new connection based on the profile settings
     // Arguments:
@@ -1233,13 +1239,7 @@ namespace winrt::TerminalApp::implementation
             // construction, because the connection might not spawn the child
             // process until later, on another thread, after we've already
             // restored the CWD to its original value.
-            const auto currentVirtualDir{ _WindowProperties.VirtualWorkingDirectory() };
-            const auto cwdString{ std::wstring_view{ currentVirtualDir } };
-            const auto requestedStartingDir{ settings.StartingDirectory() };
-            auto newWorkingDirectory = winrt::hstring{
-                Utils::EvaluateStartingDirectory(cwdString, std::wstring_view{ requestedStartingDir })
-            };
-
+            auto newWorkingDirectory{ _evaluatePathForCwd(settings.StartingDirectory()) };
             auto conhostConn = TerminalConnection::ConptyConnection();
             auto valueSet = TerminalConnection::ConptyConnection::CreateSettings(settings.Commandline(),
                                                                                  newWorkingDirectory,
@@ -4240,6 +4240,9 @@ namespace winrt::TerminalApp::implementation
             // whatever the default profile's GUID is.
 
             newTerminalArgs.Profile(::Microsoft::Console::Utils::GuidToString(profile.Guid()));
+
+            newTerminalArgs.StartingDirectory(_evaluatePathForCwd(controlSettings.DefaultSettings().StartingDirectory()));
+
             _OpenElevatedWT(newTerminalArgs);
             return true;
         }
diff --git a/src/cascadia/TerminalApp/TerminalPage.h b/src/cascadia/TerminalApp/TerminalPage.h
index f59af143fea..3eba54ac7e2 100644
--- a/src/cascadia/TerminalApp/TerminalPage.h
+++ b/src/cascadia/TerminalApp/TerminalPage.h
@@ -293,6 +293,9 @@ namespace winrt::TerminalApp::implementation
         void _OpenNewTabDropdown();
         HRESULT _OpenNewTab(const Microsoft::Terminal::Settings::Model::NewTerminalArgs& newTerminalArgs, winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection existingConnection = nullptr);
         void _CreateNewTabFromPane(std::shared_ptr<Pane> pane, uint32_t insertPosition = -1);
+
+        std::wstring _evaluatePathForCwd(std::wstring_view path);
+
         winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection _CreateConnectionFromSettings(Microsoft::Terminal::Settings::Model::Profile profile, Microsoft::Terminal::Settings::Model::TerminalSettings settings, const bool inheritCursor);
         winrt::Microsoft::Terminal::TerminalConnection::ITerminalConnection _duplicateConnectionForRestart(std::shared_ptr<Pane> pane);
         void _restartPaneConnection(const std::shared_ptr<Pane>& pane);


Test patch: