Skip to content

Avoid calling GetTempFileName in BitmapDownload#11455

Open
apoorvdarshan wants to merge 1 commit intodotnet:mainfrom
apoorvdarshan:fix/avoid-GetTempFileName-in-BitmapDownload
Open

Avoid calling GetTempFileName in BitmapDownload#11455
apoorvdarshan wants to merge 1 commit intodotnet:mainfrom
apoorvdarshan:fix/avoid-GetTempFileName-in-BitmapDownload

Conversation

@apoorvdarshan
Copy link

@apoorvdarshan apoorvdarshan commented Feb 17, 2026

Summary

  • Replace the Win32 GetTempFileName P/Invoke in BitmapDownload.BeginDownload with Path.GetRandomFileName() + CREATE_NEW in a retry loop, avoiding IOException when the folder contains 65k+ temp files
  • Add CREATE_NEW native constant and remove the now-unused GetTempFileName P/Invoke wrapper
  • Follows the same pattern already used by FileHelper.CreateAndOpenTemporaryFile in the codebase

Fixes #259

Test plan

  • Verify bitmap download from URI still works (temp file created with DELETE_ON_CLOSE semantics)
  • Verify no regressions in BitmapDecoder async download path
  • Confirm removed GetTempFileName P/Invoke has no other callers in production code
Microsoft Reviewers: Open in CodeFlow

@apoorvdarshan apoorvdarshan requested review from a team and Copilot February 17, 2026 07:26
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Feb 17, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #259 by replacing the Win32 GetTempFileName P/Invoke with Path.GetRandomFileName() + CREATE_NEW in a retry loop. The change prevents IOException when the temporary folder contains 65k+ files, which is a known limitation of the Win32 GetTempFileName API.

Changes:

  • Removed the GetTempFileName P/Invoke wrapper from UnsafeNativeMethodsOther.cs (no longer used)
  • Added CREATE_NEW constant to NativeMethodsOther.cs for proper file creation semantics
  • Replaced GetTempFileName usage in BitmapDownload.BeginDownload with a retry loop using Path.GetRandomFileName() and CREATE_NEW disposition

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/UnsafeNativeMethodsOther.cs Removed unused GetTempFileName P/Invoke wrapper and related code
src/Microsoft.DotNet.Wpf/src/Shared/MS/Win32/NativeMethodsOther.cs Added CREATE_NEW constant for Win32 CreateFile disposition
src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapDownload.cs Replaced GetTempFileName with retry loop using Path.GetRandomFileName() and CREATE_NEW; removed unused imports

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@h3xds1nz
Copy link
Member

h3xds1nz commented Feb 17, 2026

A better solution would be to directly use Path.GetTempFileName whose implementation on .NET 8+ already solved this problem.

@apoorvdarshan apoorvdarshan force-pushed the fix/avoid-GetTempFileName-in-BitmapDownload branch from 0d5645b to a9c8921 Compare February 17, 2026 23:43
@apoorvdarshan
Copy link
Author

Thanks for the suggestion @h3xds1nz! You're right — Path.GetTempFileName on .NET 8+ already handles this internally with random filenames and retry logic. I've simplified the PR to just replace the Win32 P/Invoke with Path.GetTempFileName() directly.

@h3xds1nz
Copy link
Member

h3xds1nz commented Feb 17, 2026

@apoorvdarshan It can still throw an exception, so for correctness, it should be under the exception handler; though it's very unlikely anyone's gonna be that lucky in their lifetime (but exceptional cases, hey).

Other issue is that this implementation now changes the observable behaviour as the files will be now stored in %temp%, so that should be noted.

Replace the Win32 GetTempFileName P/Invoke with Path.GetTempFileName,
whose .NET 8+ implementation avoids the 65k file limit by generating
random filenames internally.

Fixes dotnet#259
@apoorvdarshan apoorvdarshan force-pushed the fix/avoid-GetTempFileName-in-BitmapDownload branch from a9c8921 to 46df3cb Compare February 17, 2026 23:57
@apoorvdarshan
Copy link
Author

@h3xds1nz Good catch, moved it inside the try block. And yeah the location changes but since it's DELETE_ON_CLOSE it shouldn't really matter in practice.

@lindexi
Copy link
Member

lindexi commented Feb 25, 2026

Reference: #696

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid calling Path.GetTempFileName

4 participants