Skip to content

Commit e25ea70

Browse files
committed
Use path of focused explorer window as fallback
1 parent 778bbae commit e25ea70

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

PasteIntoFile/PasteIntoFile.csproj

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@
7575
<Reference Include="System" />
7676
<Reference Include="System.Configuration" />
7777
<Reference Include="System.Core" />
78-
<Reference Include="System.Xml.Linq" />
79-
<Reference Include="System.Data.DataSetExtensions" />
80-
<Reference Include="Microsoft.CSharp" />
81-
<Reference Include="System.Data" />
8278
<Reference Include="System.Deployment" />
8379
<Reference Include="System.Drawing" />
8480
<Reference Include="System.Windows.Forms" />
@@ -141,6 +137,26 @@
141137
<Content Include="Resources\icon.png" />
142138
<None Include="Resources\next.png" />
143139
</ItemGroup>
140+
<ItemGroup>
141+
<COMReference Include="SHDocVw">
142+
<Guid>{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}</Guid>
143+
<VersionMajor>1</VersionMajor>
144+
<VersionMinor>1</VersionMinor>
145+
<Lcid>0</Lcid>
146+
<WrapperTool>tlbimp</WrapperTool>
147+
<Isolated>False</Isolated>
148+
<EmbedInteropTypes>True</EmbedInteropTypes>
149+
</COMReference>
150+
<COMReference Include="Shell32">
151+
<Guid>{50A7E9B0-70EF-11D1-B75A-00A0C90564FE}</Guid>
152+
<VersionMajor>1</VersionMajor>
153+
<VersionMinor>0</VersionMinor>
154+
<Lcid>0</Lcid>
155+
<WrapperTool>tlbimp</WrapperTool>
156+
<Isolated>False</Isolated>
157+
<EmbedInteropTypes>True</EmbedInteropTypes>
158+
</COMReference>
159+
</ItemGroup>
144160
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
145161
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
146162
Other similar extension points exist, see Microsoft.Common.targets.

PasteIntoFile/Program.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void Main(string[] args)
7878
}
7979
else
8080
{
81-
Application.Run(new frmMain());
81+
Application.Run(new frmMain(GetActiveExplorerPath()));
8282
}
8383

8484
}
@@ -179,7 +179,27 @@ static RegistryKey OpenDirectoryKey()
179179
{
180180
return Registry.CurrentUser.CreateSubKey(@"Software\Classes\Directory");
181181
}
182+
183+
// get path of active or only windows explorer
184+
// modified from https://stackoverflow.com/a/5708578/13324744
185+
private static string GetActiveExplorerPath()
186+
{
187+
IntPtr handle = GetForegroundWindow();
188+
var shellWindows = new SHDocVw.ShellWindows();
189+
foreach (SHDocVw.InternetExplorer window in shellWindows)
190+
{
191+
if (window.HWND == (int) handle || shellWindows.Count == 1)
192+
{
193+
var shellWindow = window.Document as Shell32.IShellFolderViewDual2;
194+
return shellWindow?.Folder.Items().Item().Path;
195+
}
196+
}
197+
return null;
198+
}
182199

200+
[System.Runtime.InteropServices.DllImport("user32.dll")]
201+
private static extern IntPtr GetForegroundWindow();
202+
183203
[System.Runtime.InteropServices.DllImport("user32.dll")]
184204
private static extern bool SetProcessDPIAware();
185205
}

0 commit comments

Comments
 (0)